VBA Issue - Having an issue compiling this code that I got from AI and need it to run

BudZ 141 Reputation points
2026-01-25T22:09:04.59+00:00

Issue with VBA code but here is what I am trying to do

I have 2 workbooks. Workbook1 that will always be open is currently called January 2026 Hours Report. Next month it will be February 2026 Hours Report. The worksheet will always be called OtherReports. Workbook2 will always be called Project Forecasting and Planning Tool v6 and the worksheet will always be CSR Monthly View Starting in workbook January 2026 Hours Report Worksheet OtherReports, I want to run through every column A row containing alphanumeric numbers in that worksheet and match that alphanumeric number in workbook 2 worksheet CSR Monthly view column A and if it matches....... copy from worksheet CSR Monthly View the numeric value that is currently in column z of that row and Paste it into January 2026 Hours Report worksheet OtherReports Column G. The worksheet names will never be duplicated in each others workbook as the worksheets are all defined

The AI code can be tossed

    Getting syntax errors on what AI produced on all the For Loop section and the    Set targetRange = targetSheet.Range("A1:A100") ' Adjust range as needed

The following is what AI produced Public Sub UserSortInput() Dim userInput As String Dim promptMsg As String Dim tryAgain As Integer Dim SourceWorkbook As Workbook Dim sourceSheet As Worksheet Dim sourceRange As Range Dim targetWorkbook As Workbook Dim targetSheet As Worksheet Dim targetRange As Range

' Set the source and target workbooks
Set SourceWorkbook = Workbooks.Open("C:\_TTS_MACRO\01_Reports\Monthly Reports\2026\January Issues\Project Forecasting and Planning Tool v6.xlsm")
Set sourceSheet = SourceWorkbook.Sheets("CSR MONTHLY VIEW")

' sourceSheet.Activate 'Activate the worksheet to update CSR Monthly View 'method that allows you to activate the specified worksheet SourceWorkbook.Worksheets("CSR MONTHLY VIEW").Activate ActiveWindow.Zoom = 100 'Change Worksheet Zoom to 100

Set targetWorkbook = Workbooks.Open("C:\_TTS_MACRO\01_Reports\Monthly Reports\2026\January Issues\January 2026 Hours Report.xlsm")
Set targetSheet = targetWorkbook.Sheets("OtherReports")
targetWorkbook.Worksheets("OtherReports").Activate

Set sourceRange = sourceSheet.Range("A1:A500") ' Adjust range as needed

   Set targetRange = targetSheet.Range("A1:A100") ' Adjust range as needed       ' Loop through each cell in the source range    For Each cell In targetRange        ' Find the matching cell in the source range        Set foundCell = sourceRange.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)        If Not foundCell Is Nothing Then            ' Update data in the target workbook            foundCell.Offset(0, 6).Value = cell.Offset(0, 25).Value ' Adjust offset as needed        End If    Next cell End Sub

Microsoft 365 and Office | Excel | For business | Windows
{count} votes

2 answers

Sort by: Most helpful
  1. Barry Schwarz 5,331 Reputation points
    2026-01-26T08:12:47.0633333+00:00

    After fixing the line breaks in how your message was presented, the only compile errors were due to cell and foundcell not appearing in a Dim statement. Each should be defined as a range.

    Whether the code will do what you want is a different question. We would need sample data if you want help with that. Best would be to upload the workbooks to some storage domain where we could download it and test.

    0 comments No comments

  2. Darren-Ng 7,945 Reputation points Microsoft External Staff Moderator
    2026-01-26T07:10:01.8733333+00:00

    Dear @BudZ,

    Thank you for posting your question in the Microsoft Q&A forum.

    Based on your description, this issue is related to VBA code compilation and debugging, which goes beyond the scope of Microsoft Learn Q&A. The Learn Q&A platform primarily focuses on product usage, configuration, and supported scenarios, and it is not optimized for in‑depth code review or troubleshooting complex VBA logic.

    For more specialized help with VBA development, I recommend posting your question on one of the following communities, where there are dedicated experts who regularly assist with code‑level issues:

    https://techcommunity.microsoft.com/ and https://stackoverflow.com/questions

    If you have any other questions, please feel free to reach out, I'm glad to support you again. Thank you for your understanding

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.