Reputation: 1
Hi there I have no background knowledge in to how to build an VBA scipt. i have the following requirments for my script The script should scan per user to find which numbers are associated with that user. The numbers that could be associated consist of numbers numbered between 1 and 23 with an optional "+" or "-" sign. Specifically, the following numbers can be found (1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10, 11, -11, 12, -12, 13, -13, 15, -15, 16, -16, 18, -18, 20, -20, 21, -21, 22, -22, 23, -23). The script reads each row to determine which numbers appear in that row under columns AQ, AU, AY, BC, BE, BV, BZ, CD, CF. This is done for rows 5 to 75. After scanning all the numbers that appeared, the script then moves to columns CQ to DT. For the following numbers, a 1 should be placed in the next column if found in the respective row, and a 0 if the code is not found in the respective row: 1 in CG, -1 in CH, 2 in CI, -2 in CJ, 3 in CK, -3 in CL, 4 in CM, -4 in CN, 5 in CO, -5 in CP, 6 in CQ, -6 in CR, 7 in CS, -7 in CT, 8 in CU, -8 in CV, 9 in CW, -9 in CX, 10 in CY, -10 in CZ, 11 in DA, -11 in DB, 12 in DC, -12 in DD, 13 in DE, -13 in DF, 15 in DG, -15 in DH, 16 in DI, -16 in DJ, 18 in DK, -18 in DL, 20 in DM, -20 in DN, 21 in DO, -21 in DP, 22 in DQ, -22 in DR, 23 in DS, -23 in DT. Example: If the number "7" appears in row 8 under column AQ, then a "1" is placed in row 7 under column CS. If, for example, the number "9" did not appear in row 8 under the aforementioned columns (AQ, AU, AY, BC, BE, BV, BZ, CD, CF), then a "0" is placed in row 8 under column CW. If a number appears multiple times in a row, the script should not place a "1" under the corresponding column for that number but instead should place the number of occurrences. For example, if the number "4" appears 3 times in row 17, the script places the number "3" under column CM in row 17. The script should account for sensitivity to "+" and "-" signs that may precede a number.
I've asked AI and AI has come up with the following script
Sub ProcessDataAndPopulateResults()
Sub ProcessDataAndPopulateResults()
Dim wb As Workbook
Dim ws As Worksheet
Dim rowNum As Integer
Dim col As Integer
Dim cellValue As String
Dim number As Integer
Dim numCount As Integer
Set wb = Workbooks.Open("document name, for privacy reasos I've removed this")
Set ws = wb.Sheets(1)
For rowNum = 5 To 75
For Each cell In ws.Range("AQ" & rowNum & ",AU" & rowNum & ",AY" & rowNum & ",BC" & rowNum & ",BE" & rowNum & ",BV" & rowNum & ",BZ" & rowNum & ",CD" & rowNum & ",CF" & rowNum)
If Not IsEmpty(cell.Value) Then
cellValue = CStr(cell.Value)
If Len(cellValue) > 0 Then
If IsNumeric(cellValue) Then
number = CInt(cellValue)
If number >= 1 And number <= 23 Then
numCount = WorksheetFunction.CountIf(ws.Range("AQ" & rowNum & ":CF" & rowNum), number)
ws.Cells(rowNum, 83 + number + IIf(InStr(cellValue, "-") > 0, 1, 0)).Value = numCount
End If
End If
End If
End If
Next cell
Next rowNum
For rowNum = 5 To 75
For Each cell In ws.Range("CQ" & rowNum & ":DT" & rowNum)
If Not IsEmpty(cell.Value) Then
cellValue = CStr(cell.Value)
If Len(cellValue) > 0 Then
If IsNumeric(cellValue) Then
number = CInt(cellValue)
If number >= 1 And number <= 23 Then
If InStr(cellValue, "-") > 0 Then
ws.Cells(rowNum, 83 + number + 1).Value = 0
Else
ws.Cells(rowNum, 83 + number).Value = 1
End If
End If
End If
End If
End If
Next cell
Next rowNum
wb.Close SaveChanges:=True
End Sub
Only when I try to run my script I get the error code " Compile error: Invalid outside procedure"
What can I do to improve my code?
I've tried to save my code under ProcessDataAndpopulateResults but when i try to save it the code will not run
Upvotes: 0
Views: 41