swartzfeger
New Member
- Joined
- May 23, 2022
- Messages
- 17
- Platform
- Windows
- MacOS
All, I'm having some issue with VBA I wrote. I tried fixing it with CHatGPT and it was one step forward, two steps back -- some things improved and a lot more broke! lol
Here's what I'm trying to do. Simple spreadsheet like this:
A column ("Batch) with a range of numbers, and then the next column will track which numbers in that previous batch are missing. What I need to do is export the range of missing numbers, but prepend each missing number with the name of its associated batch (the name of the previous column. So the export would look like this:
The challenge -- these worksheets will always have an undetermined number of batches, so there will always be an undetermined number of "Missing #" columns. We need to find every Missing # column that's no empty, and export that number prepended with the previous column's header name.
Unfortunately, my VBA is only exporting the first "Missing #" column. ChatGPT helped a little but sometimes it only exported the final Missing # column; one time it exported all the columns, but into separate files. One time it got close and managed to grab all the missing numbers, but it only prepended the Batch number with the missing number for the first row of the Missing # column. Here's the VBA:
The other issue (that I can eventually figure out and not as important) is that it prepends it like "- Batch 1 - 123" with a leading -. Any ideas? I'm starting to go cross-eyed looking at this!
Here's what I'm trying to do. Simple spreadsheet like this:
Batch 1 | Missing # | Batch 2 | Missing # | Batch 3 | Missing # |
1 | 4 | 11 | 12 | 21 | 26 |
2 | 6 | 12 | 17 | 22 | 30 |
3 | 9 | 13 | 23 | ||
4 | 14 | 24 | |||
5 | 15 | 25 | |||
6 | 16 | 26 | |||
7 | 17 | 27 | |||
8 | 18 | 28 | |||
9 | 19 | 29 | |||
10 | 20 | 30 | |||
A column ("Batch) with a range of numbers, and then the next column will track which numbers in that previous batch are missing. What I need to do is export the range of missing numbers, but prepend each missing number with the name of its associated batch (the name of the previous column. So the export would look like this:
Batch 1 - 4 |
Batch 1 - 6 |
Batch 1 - 9 |
Batch 2 - 12 |
Batch 2 - 17 |
Batch 3 - 26 |
Batch 3 - 30 |
The challenge -- these worksheets will always have an undetermined number of batches, so there will always be an undetermined number of "Missing #" columns. We need to find every Missing # column that's no empty, and export that number prepended with the previous column's header name.
Unfortunately, my VBA is only exporting the first "Missing #" column. ChatGPT helped a little but sometimes it only exported the final Missing # column; one time it exported all the columns, but into separate files. One time it got close and managed to grab all the missing numbers, but it only prepended the Batch number with the missing number for the first row of the Missing # column. Here's the VBA:
VBA Code:
Sub ExportMissingData()
Dim ws As Worksheet
Dim rng As Range
Dim savePath As Variant
Dim wb As Workbook
Dim prevHeader As String
Dim fileName As String
Dim missingColumns As New Collection
Dim col As Variant
Dim i As Long
Set ws = ActiveSheet
For Each cell In ws.Range("A1").CurrentRegion.Rows(1).Cells
If InStr(cell.Value, "Missing #") > 0 Then
missingColumns.Add cell.Column
End If
Next cell
If missingColumns.Count > 0 Then
For Each col In missingColumns
Set rng = ws.Range(ws.Cells(2, col), ws.Cells(ws.Rows.Count, col).End(xlUp))
fileName = "Export_" & Format(Date, "mm-dd-yyyy") & "_" & Format(Time, "hhmmss") & ".xls"
savePath = Application.DefaultFilePath & "\" & fileName
Set wb = Workbooks.Add
prevHeader = ""
For i = 1 To col - 1
prevHeader = prevHeader & " - " & ws.Cells(1, i).Value
Next i
rng.Copy Destination:=wb.Worksheets(1).Range("A1")
For i = 1 To rng.Rows.Count
wb.Worksheets(1).Cells(i, 1).Value = prevHeader & " - " & wb.Worksheets(1).Cells(i, 1).Value
Next i
Application.DisplayAlerts = False
wb.SaveAs fileName:=savePath, FileFormat:=56
wb.Close
Application.DisplayAlerts = True
Next col
End If
End Sub
The other issue (that I can eventually figure out and not as important) is that it prepends it like "- Batch 1 - 123" with a leading -. Any ideas? I'm starting to go cross-eyed looking at this!