So Im trying to create a VBA that copies data from 4 sheets that have the same structure into another sheet and in that sheet I have extra two columns YearMonth and SourceSheet. The sourcesheet column should contain info from which of the 4 sheets the data is from and yearmonth is populated based on the selected month from cell F1 of calculation sheet. Problem is that the yearmonth and sourcesheet columns continue putting data all the way down where they shouldn't be. So for example row 298 is the last one and all other columns are fine except the sourcesheet and yearmonth which continue to row 400 something for some reason.
Im mainly using chatgpt for help as Im beginner with VBA and it still gives me a code that has the same issue. Can you help me what is wrong?
The code is:
Sub CopyDataToDataSheet()
Dim dataSheet As Worksheet
Dim inputSheet As Worksheet
Dim calculationSheet As Worksheet
Dim lastRow As Long
Dim yearMonthValue As Variant
Dim confirmation As Integer
Dim yearMonthColumn As Range
Dim sourceSheetColumn As Range
Dim dataRange As Range
Dim sourceSheetNames As Variant
Dim dataLastRow As Long`
' Set the data sheet
Set dataSheet = ThisWorkbook.Worksheets("Data Archive")
' Unfilter the data sheet
If dataSheet.AutoFilterMode Then
dataSheet.AutoFilterMode = False
End If
' Set the calculation sheet
Set calculationSheet = ThisWorkbook.Worksheets("Calculation")
' Find the column index of "SourceSheet" in the data sheet
Set sourceSheetColumn = dataSheet.Rows(1).Find("SourceSheet", LookIn:=xlValues, LookAt:=xlWhole)
' Loop through the input sheets
sourceSheetNames = Array("sheet1", "sheet2", "sheet3", "sheet4")
For Each inputSheet In ThisWorkbook.Worksheets(sourceSheetNames)
' Find the last row in the data sheet
lastRow = dataSheet.Cells(dataSheet.Rows.Count, "A").End(xlUp).Row
' Copy the data from the input sheet to the data sheet
Set dataRange = inputSheet.UsedRange.Offset(1)
dataRange.Copy dataSheet.Cells(lastRow + 1, "A")
' Get the value from cell F1 of the Calculation sheet
yearMonthValue = calculationSheet.Range("F1").Value
' Check if "SourceSheet" column exists
If Not sourceSheetColumn Is Nothing Then
' Find the last row of imported data in column A
dataLastRow = lastRow + dataRange.Rows.Count
' Assign the source sheet name to the "SourceSheet" column in the data sheet for each row
dataSheet.Range(dataSheet.Cells(lastRow + 1, sourceSheetColumn.Column), dataSheet.Cells(dataLastRow, sourceSheetColumn.Column)).Value = inputSheet.Name
' Clear the remaining cells in the "SourceSheet" column below the imported data
dataSheet.Range(dataSheet.Cells(dataLastRow + 1, sourceSheetColumn.Column), dataSheet.Cells(dataSheet.Rows.Count, sourceSheetColumn.Column)).ClearContents
End If
' Find the column index of "YearMonth" in the data sheet
Set yearMonthColumn = dataSheet.Rows(1).Find("YearMonth", LookIn:=xlValues, LookAt:=xlWhole)
' Check if "YearMonth" column exists
If Not yearMonthColumn Is Nothing Then
' Assign the yearMonthValue to the "YearMonth" column in the data sheet for each row
dataSheet.Range(dataSheet.Cells(lastRow + 1, yearMonthColumn.Column), dataSheet.Cells(dataLastRow, yearMonthColumn.Column)).Value = yearMonthValue
' Clear the remaining cells in the "YearMonth" column below the imported data
dataSheet.Range(dataSheet.Cells(dataLastRow + 1, yearMonthColumn.Column), dataSheet.Cells(dataSheet.Rows.Count, yearMonthColumn.Column)).ClearContents
End If
' Delete the data from the input sheet
' dataRange.ClearContents
Next inputSheet
' Display success message
MsgBox "Data imported successfully."
End Sub`