vba code pasting into the wrong sheet

wingers13

New Member
Joined
Jun 25, 2023
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
Hi,
I'm looking for some advice with a new macro I have made.

I've previously created a number of macros which open files, copy data and then process/paste it into another sheet. This time I'm somehow not able to paste the data into the correct sheet however.

I've trimmed the code down below to show the core issue in a simplified form.
The data is copied from a csv file with only one sheet in it, and then should be pasted into the "Data" sheet in my main workbook. The problem is despite closing the csv file and activating the Data sheet, the data is still pasted into a different sheet in the main workbook.

Any help on this would be much appreciated.
Thanks in advance,
Wingers

VBA Code:
    Dim StrFile As String
    Dim Folder As String
    Dim SessionName As String
    Dim Current As Workbook
    Dim csv_file As Workbook
    
    'Set Starting values
    Application.ScreenUpdating = False
    Set Current = ActiveWorkbook
    Folder = Range("Folder").Value
    StrFile = Dir(Folder)
    
    Do While Len(StrFile) > 0
        Workbooks.Open Filename:=Folder & StrFile
        Set csv_file = ActiveWorkbook
        
        'Collect data
        SessionName = Left(StrFile, Len(StrFile) - 4)
        
        csv_file.Close
        Current.Activate
        Sheets("Data").Activate
        
        'Paste data
        Cells(5, 1).Value = SessionName
        
        StrFile = Dir
    Loop
    
    
    Application.ScreenUpdating = True
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I should say that this is the same approach I have used successfully in other projects, which makes it all the more confusing...
 
Upvote 0
What happens with
VBA Code:
        SessionName = Left(StrFile, Len(StrFile) - 4)
        
        csv_file.Close
        Current.Sheets("Data").Cells(5, 1).Value = SessionName
and is Current the workbook the code resides in?
 
Upvote 0
What happens with
VBA Code:
        SessionName = Left(StrFile, Len(StrFile) - 4)
       
        csv_file.Close
        Current.Sheets("Data").Cells(5, 1).Value = SessionName
and is Current the workbook the code resides in?
Yes, "current" is the main workbook with the code in it.
"SessionName" is a variable that is collected from the csv file, trimmed to make it more useful, and pasted into the new sheet.
For now I've just put it into cell A5 as a test, but it doesnt appear on the Data sheet. Instead it appears in the first sheet of the file.

Wingers
 
Upvote 0
Try a Select instead, if only for debugging?
I would also walk the code.

Code:
Sheets("Destinations").Select
 
Upvote 0
For now I've just put it into cell A5 as a test, but it doesnt appear on the Data sheet.
Is that with the code that Mark just posted? That should not be possible.

I suspect your code is in the worksheet module for the sheet where the "paste" is actually happening.
 
Upvote 0
I tried Select and still no difference. When walking through the code the displayed sheet swapped over but the data still went into the first sheet.

Rory hit the nail on the head though. I'd put the code into the sheet module.l by mistake. Now it's moved and all working correctly. Not one I've come across before but every day's a school day.

Thanks gents.
 
Upvote 0
I tried Select and still no difference
Mark's code didn't use select: it fully qualified the Cells property with the relevant worksheet. No need to select or activate. ;)
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,123,009
Members
449,093
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top