Need help with a macro that will open 5 different csv files and automatically auto copy paste 3 columns of data (starting from the 2nd row to about the 200th row). Then the data will be pasted into one worksheet that is open so each file is all on one row (side by side)...any help will be appreciated..
Sub Macro2()
'Assign variable name to Target workbook
Var1 = ActiveWorkbook.Name
'Assign variable name to Target range
Var1R = "H1"
'Open Source WorkBook
Application.Workbooks.Open ("C:\MY DOCUMENTS\WORKBOOK(B).xls")
'Assign variable name to Source workbook
Var2 = ActiveWorkbook.Name
Var2R = "WORKSHEET-1"
'Copy from Source to Target
Sheets(Var2R).Columns("F").EntireColumn.Copy _
Destination:=Workbooks(Var1).Sheets("Sheet1").Range(Var1R)
'Close Source WorkBook wo/Save
Workbooks(Var2).Close False
End Sub
Here is a program that will do that. Obviously you'll have to modify the file-paths and ranges.
Sub copy_paste()
Dim filepaths
Dim twb As Workbook
Dim x As Long
Set twb = ThisWorkbook
filepaths = Array("C:\A.csv", "C:\B.csv", "C:\C.csv", "C:\D.csv", "C:\E.csv")
For x = 1 To UBound(filepaths)+1
With Workbooks.Open(filepaths(x-1))
.Sheets(1).Range("A2:C200").Copy twb.Sheets(1).Cells(2, 3 * x - 2)
.Close False
End With
Next x
End Sub
Related
I'm hoping someone can help with this but I'm having the darnest time getting anything to work.
I have a rather large workbook with lots of worksheets, I have a report that runs and populates Column B with a "trigger"
Column A: is the name of all the worksheets in the workbook. Column B is the indicator that the specific worksheet needs to be moved, e.g. "Yes". I need to move the specified workbook into another workbook.
I can only find applicable examples for moving cells but it didn't work. Any help or direction will be greatly appreciated!
Dim WBK As Workbook
Dim WBK2 As Workbook
Set WBK= ThisWorkbook
Set WBK= Workbooks.Open(Filename:"ReportList.xlsx")
For i = 1 To Sheets("MoveSheet").End(xlDown).Row '(ERRORHERE)
If Sheets("MoveSheet").Range("B" & i) = "Move" Then
Sheets(Sheets("MoveSHeet").Range("A" & i)).Move After:=wkbk2.Sheets(1)
Else
End if
Next i
End Sub
Your posted code is not too far off - a few typos etc
Try this:
Sub Tester()
Dim wb As Workbook, wsList As Worksheet, c As Range
Dim wbDest As Workbook
Set wb = ThisWorkbook
Set wsList = wb.Worksheets("MoveSheet") 'your sheet with tab names and "Move" flag
Set wbDest = Workbooks.Open(Filename:="C:\Example\Path\ReportList.xlsx") 'provide the full path
For Each c In wsList.Range("A1:A" & wsList.Cells(Rows.Count, "A").End(xlUp).Row).Cells
If c.Offset(0, 1).Value = "Move" Then 'has a flag to be moved?
wb.Worksheets(c.Value).Move after:=wbDest.Sheets(wbDest.Sheets.Count) 'move after last sheet
End If
Next c
End Sub
Maybe a for each loop would be good.
Dim wkbk1 As Workbook - Main workbook
Dim wkbk2 As Workbook - Your other workbook
Set wkbk1 = ActiveWorkbook
Set wkbk2 = "input name here"
Dim ws As Worksheet
For Each ws In wkbk1.Sheets
'use if code to check if certain Criteria met'
ws.Move wkbk2.Sheets(Sheets.Count)
Next ws
I would have one loop to run through column A and check if the sheet needs to be move by checking the column B right alongside it. If column B contains the trigger, which would be checked with a if condition, then move the sheet to another workbook.
For i = 1 To Sheets("Sheet1").Range("A1").End(xlDown).Row
If Sheets("sheet1").Range("B" & i) = "Yes" Then
Sheets(Sheets("sheet1").Range("A" & i)).Move After:=Workbooks("Otherworkbook.xls").Sheets(1)
Else
End If
Next i
Something like this but might need to declare the second workbook with the full filename and directory.
I have some VBA code which I use in another workbook to resize a table to be 1 row and delete the contents of a data table to initialize a workbook. Then a file prompt opens asking the user to select the appropriate file for processing. For some reason, I am getting a
"Run-time error '91': Object variable or With block variable not set"
The code is a copy and paste from the other workbook and I have adjusted the names of the variables, workbooks, worksheets, and table names.
workbook is called "IMD Processing.xlsm" with 2 sheets titled "IMD" and "Raw". The "Raw" sheet has a table with the name "tbl_raw" and the "IMD" sheet has a table with the name "tbl_imd".
Any guidance would be greatly appreciated.
Option Explicit
Sub IMDAutomation()
Dim fileName As String 'Filename string
Dim wb_macro As Workbook 'Macro workbook
Dim ws_macro_imd As Worksheet 'Macro worksheet
Dim ws_macro_raw As Worksheet 'Macro raw worksheet
Dim wb_imd As Workbook 'IMD Workbook for processing
Dim ws_imd As Worksheet 'IMD Worksheet for processing
Dim objTable As ListObject 'Table of raw data
Dim tbl_raw As ListObject 'Raw table in macro workbook
Dim tbl_imd As ListObject 'IMD table in macro workbook
Dim vals As Variant 'Array to store values
Dim lrow As Long 'Variable used to determine number of rows in data table
Set wb_macro = ThisWorkbook
Set ws_macro_imd = Sheets("IMD")
Set ws_macro_raw = Sheets("Raw")
'============ Initialize macro workbook - clearing data ============'
'Clear the raw data in the macro workbook
Set tbl_raw = ws_macro_raw.ListObjects("tbl_raw")
With tbl_raw.DataBodyRange
If .Rows.Count > 1 Then
.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
End If
End With
tbl_raw.DataBodyRange.Rows(1).ClearContents
'Clear the IMD data in the macro workbook
Set tbl_imd = ws_macro_imd.ListObjects("tbl_imd")
With tbl_imd.DataBodyRange
If .Rows.Count > 1 Then
.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
End If
End With
'============ Locate Raw Data File ============'
'Open file dialog to locate the Workforce Review raw data workbook exported from system
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select the IMD file"
.Filters.Clear
.Filters.Add "Custom Excel Files", "*.xlsx, *xls, *csv"
.Show
fileName = .SelectedItems.Item(1)
End With
If InStr(fileName, ".xlsx") = 0 Then
Exit Sub
End If
Workbooks.Open fileName
'Set the Workforce Review raw workbook
Set wb_imd = ActiveWorkbook
'Set the worksheet
Set ws_imd = wb_imd.ActiveSheet
lrow = ws_imd.Cells(ws_imd.Rows.Count, 2).End(xlUp).Row
vals = ws_imd.Range("A2:CU" & lrow)
Application.CutCopyMode = False
Application.CutCopyMode = True
End Sub
UDPATE WITH SOLUTION
Thanks to #Variatus for the solution.
I did not have a data row in my table so I created one and now it's working.
This should work to handle cases where there is no row in the table. If tbl_raw.DataBodyRange Is Nothing Then InsertRowRange Else (Code to clear the table)
Probably the object that is being searched for by Set tbl_raw = ws_macro_raw.ListObjects("tbl_raw") does not exist in the new workbook and hence referencing through With tbl_raw returns this error
At "New folder" I have the excel workbooks which will be opened by the loop; I want to copy 2 columns in each of these workbooks and paste it in another workbook called "new"
When I run the code I get the Run-time error '91': Object variable or With block variable not set
at line With wb.Worksheets(5) and only data of the first workbook are copied.
How can I fix it?
Option Explicit
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Dim wbMain As Workbook
Dim i As Integer
Set wbMain = Workbooks.Open("C:\Users\A\Desktop\VBA\new.xlsx")
Pathname = "C:\Users\A\Desktop\VBA\New folder\"
Filename = Dir(Pathname)
i = 1
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
Enter_Formulas wb, wbMain, i
wb.Close SaveChanges:=True
Filename = Dir()
i = i + 2
Loop
End Sub
Sub Enter_Formulas(wb As Workbook, wbMain As Workbook, i)
With wb.Worksheets(5)
.Columns(1).Copy Destination:=wbMain.Worksheets(2).Columns(i)
.Columns(3).Copy Destination:=wbMain.Worksheets(2).Columns(i + 1)
End With
End Sub
You are telling VBA to copy columns A and C from the 5th worksheet of the opened workbook. Seems like it doesn't have five or more sheets.
I am looking for a macro to paste some data onto a moving range. I already have a cell that tells me the number of the next non empty column and this is the code I currently use:
Dim OpenFileName As String
Dim wb As Workbook
'Select and Open workbook
OpenFileName = Application.GetOpenFilename()
If OpenFileName = "False" Then Exit Sub
Set wb = Workbooks.Open(OpenFileName)
'Get data EXAMPLE
ThisWorkbook.Sheets("Teleselling 17").Range("I9:I289")*this should be dynamic, I want to paste data in a moving range*.Value = wb.Sheets("TELESELLING INBOUND").Range("L9:L289").Value
wb.Close SaveChanges:=False
MsgBox ("Done!")
Use the newly opened workbook/worksheet/range to define the scope of the value transfer.
with wb.workSheets("TELESELLING INBOUND").Range("L9:L289")
ThisWorkbook.workSheets("Teleselling 17").Range("XFD9").end(xltoleft).offset(0, 1).resize(.rows.count, .columns.count) = .value
end with
I have N number of sheets in an excel file. now I want to convert all of the sheets into .csv format Using bash shell scripting And before converting the xlxs file to .csv I need to add extra columns to each sheet with common value at the end. Please help
I agree with the other comment, can't this be done through excel?
Example solution if you choose this approach:
' Forces all variables to be declared
Option Explicit
Sub WorksheetLoop()
' Define variable types
Dim WS_Count As Integer
Dim I As Integer
Dim currentWorkSheet As String
Dim PathName As String
' Stop screen from flickering while code is running
Application.ScreenUpdating = False
' Set WS_Count equal to the number of worksheets in the active workbook
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
' Find name of the current sheet
currentWorkSheet = ActiveWorkbook.Worksheets(I).Name
' Go to the current sheet
Worksheets(currentWorkSheet).Activate
' Add extra column on that sheet
' Select rows in the extra column where the formula is to be added
Range("D1:D10").Select
' Add the formula (example formula, multiplying previous column by 1)
Selection.FormulaR1C1 = "=RC[-1]*1"
' Export to CSV
' Copy the current worksheet
ActiveWorkbook.Sheets(currentWorkSheet).Copy after:=ActiveWorkbook.Sheets(currentWorkSheet)
' Rename the current worksheet
Sheets(ActiveSheet.Name).Name = currentWorkSheet & "_export"
' Export the excel to csv
' Create path and name for export
PathName = "" & ThisWorkbook.Path & ActiveSheet.Name & ".csv"
' Move sheet to seperate workbook
Sheets(ActiveSheet.Name).Move
' Save as CSV file
ActiveWorkbook.SaveAs Filename:=PathName, FileFormat:=xlCSV
' Close that CSV with the SAVE warnings surpressed
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Next I
Application.ScreenUpdating = True
End Sub
To run the code use the vba editor (shortcut to open the editor ALT+F11). Open a new macro, paste the code, then you can use F8 to step through each line