Thank you in advance for the help with this question. I have a set of workbooks who have varying numbers of worksheets. On each worksheet, there are 6 cells of information that I need to have compiled onto one master worksheet in that workbook. The 6 cells of information are on the same page of every worksheet. My final output would be a table on one worksheet that has all of the data from the other worksheets in that book. I can do it manually but was hoping that a macro would help. I've tried using the activeworkbook.worksheet identifier but am stuck. Thanks for any help.
I was going to suggest the same thing #TheEngineer did because your question sounded too general and the only answer seemed to be for someone to code a complete solution for you.
However, reading a little closer, I think what you are asking for is the loop I've given you below.
Sub Macro1()
'
' Macro1 Macro
'
'
' 6 "working variables" to temporarily hold the values from each worksheet
Dim sCell1 as String
Dim sCell2 as String
Dim sCell3 as String
Dim sCell4 as String
Dim sCell5 as String
Dim sCell6 as String
Dim iTargetRowIdx as Integer 'counter to point at next empty row in target sheet
iTargetRowIdx = 1 'NOTE: change this if your 1st target row is not 1
For Each Sheet In ActiveWorkbook.Sheets
'avoid copying from the target worksheet itself
If Sheet.Name <> "<your target sheet name>" Then
'copy value from each of 6 cells to our working variables
'INSTRUCTION: replace each pair of "<row>" and "<col>" with row and column numbers of each of your 6 cells
sCell1 = Sheet.Cells(<row>, <col>) ' 1st cell to copy
sCell2 = Sheet.Cells(<row>, <col>) ' 2nd cell to copy
sCell3 = Sheet.Cells(<row>, <col>) ' 3rd cell to copy
sCell4 = Sheet.Cells(<row>, <col>) ' 4th cell to copy
sCell5 = Sheet.Cells(<row>, <col>) ' 5th cell to copy
sCell6 = Sheet.Cells(<row>, <col>) ' 6th cell to copy
'NOTE: if you want to see the value of what's in a cell while the macro is running, you can use Debug.Print, which prints to your Immediate Window (the little panel) at the bottom of the VBA editor) e.g.
Debug.Print (Sheet.Cells(1, 1))
'this prints what is in cell A1
'NOTE: Debug.Print is just a debugging tool to help you see what is going on inside your macro - you can safely remove the statement altogether
End If
'then put code here to paste those values to the next row in your target worksheet
'assumes you want the 6 values pasted to cols A to F in each new row
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 1) = sCell1
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 2) = sCell2
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 3) = sCell3
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 4) = sCell4
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 5) = sCell5
Worksheets("<your target sheet name").Cells(iTargetRowIdx, 6) = sCell6
iTargetRowIdx = iTargetRowIdx + 1 'point to next empty row
Next Sheet
End Sub
This loops through every sheet in your workbook. Inside the loop, you do all the work you described. You need to edit the code for your specifics.
You didn't say if you want to run one macro for ALL workbooks or if just on one workbook. This macro runs for only one workbook and that is the workbook where you place this macro. You can put this macro in every workbook that you want it to work in OR you can post a separate question about how to run one macro on many workbooks.
Related
I have code that will open a closed workbook, copy whole data from Worksheet "SM" and paste as values into target workbook "Paste SM" worksheet.
The data comes most of the time with some columns merged. After it has been copied and pasted as values, all merged cells are unmerged and there are some empty rows that need to be filled with duplicates.
Since I do not always receive documents with cells merged the code needs to work within scenarios:
Source Worksheet include merged cell through columns.
EX A1:A11 with value "QWERTY", A12:A23 with value "12345" etc.
Copied and paste as values - all unmerged - A1 - "QWERTY" A2-A11 blanks no value, A12 - "12345" A13-A23 blanks no value.
All blank cells to fill with duplicates, check whole worksheet.
Source Worksheet contains unmerged and merged cells.
Merged cells - same logic as for point 1.
Unmerged cells would look like A1 - "QWERTY" A2-A11 blanks no value, A12 - "12345" A13-A23 blanks no value in source worksheet.
It will remain same after copy and paste as values.
All blank cells to fill with duplicates, check whole worksheet.
Option Explicit
Public Sub Import_SM_DataFromAnotherWorkbook_and_Paste_As_Values()
' Get workbook...
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ThisWorkbook
' get the customer workbook
Dim Filter As String
Filter = "Text files (*.xlsb),*.xlsb,(*.xlsx),*.xlsx"
Dim Caption As String
Caption = "Please Select an input file "
Dim Ret As Variant
Ret = Application.GetOpenFilename(Filter, , Caption)
If VarType(Ret) = vbBoolean And Ret = False Then Exit Sub
Dim wb As Workbook
Set wb = Workbooks.Open(Ret)
'It will open source woorkbook, copy whole sheet and paste as values into current workbook. Will
close sourcebook afterwards
targetWorkbook.Worksheets("Paste SM").Range("A1").Resize(wb.Worksheets("Security Matrix").UsedRange.Rows.Count, wb.Worksheets("Security Matrix").UsedRange.Columns.Count).Value = wb.Worksheets("Security Matrix").UsedRange.Value
'close opened workbook without saving
wb.Close SaveChanges:=False
End Sub
instead of copy and paste all the new data to a new worksheet, you should create an array with all the data. Check all the values of the array and if it's empty, copy the value from the row above.
Option Base 1 'make the array starts at 1 instead of 0. This is helpful because you are working as it was a range
Dim ArrayNewData(), NewRange As Range, RangeNewData As Range
Dim i%, j%
Sub Test()
'when open the new worksheet
With wb.Sheets("SM")
Set RangeNewData = .Cells(1, 1).CurrentRegion 'set the range you want to copy
End With
ArrayNewData = RangeNewData 'save the copied range to an array.
'now you can close the wb
wb.Close SaveChanges:=False
'The size of the array is
'Rows:lbound(ArrayNewData) x ubound(ArrayNewData)
'Columns: lbound(application.transpose(ArrayNewData)) x ubound(application.transpose(ArrayNewData))
'all the "cells" in the array aren't merged
'so now, you can fill the empty "cells" of the array with the value above
For i = 2 To UBound(ArrayNewData) 'check all the rows starting from the 2nd (just because if the 1st one is empty, there's nothing to copy above
For j = 1 To UBound(Application.Transpose(ArrayNewData)) 'and check all the columns
If IsEmpty(ArrayNewData(i, j)) = True Then 'if the cell is empty...
ArrayNewData(i, j) = ArrayNewData(i - 1, j) 'the value will be the one row above
End If
Next j
Next i
'Now you've got the array of values ArrayNewData.
'Now set a new range to where you want to copy them. Maybe, you don't need to copy all of them, andkeep working with the array.
'The more interactions you do between VBA and excel, the more slow will work everything.
With Sheets("Paste SM")
Set NewRange = .Range(.Cells(1, 1), .Cells(UBound(ArrayNewData)), UBound(Application.Transpose(ArrayNewData)))
End With
NewRange = ArrayNewData
End Sub
I'd like to run a macro that's reading all sheet names from an XLS file, and then being able to run the macro from the Run Command.
How is it possible?
For the moment I have the following code but I'd like to generalise it to other files (basically inputting the filename as a parameter in this macro and command).
Sub FnGetSheetsName()
Dim mainworkBook As Workbook
Set mainworkBook = ActiveWorkbook
For i = 1 To mainworkBook.Sheets.Count
'Either we can put all names in an array , here we are printing all the names in Sheet 2
mainworkBook.Sheets("Sheet2").Range("A" & i) = mainworkBook.Sheets(i).Name
Next i
End Sub
Create a macro enabled workbook.
Use one of the cell in Sheet1 to specify the path to excel file whose sheet names you want.
In Sheet1, insert an Active X Control button using developer mode.
In the code behind of the button (double click on the button keeping design mode enabled in developer tab -> VBA window opens), add following VB code and save it:
Private Sub CommandButton1_Click()
Dim sheetCount As Integer
Dim mainWorkBook, workbook1 As Workbook
Set mainWorkBook = ActiveWorkbook
Set workbook1 = Workbooks.Open(Range("A2"))
'Clear the contents of sheet 2, starting from row 2 - verify the last cell in sheet
Selection = mainWorkBook.Sheets("Sheet2").Range("A2:ZZ104857").ClearContents
If mainWorkBook.Sheets("Sheet1").Range("A2") = "" Then
MsgBox ("Enter Excel path")
GoTo exit1
End If
For sheetCount = 1 To workbook1.Sheets.Count
'Put sheet names of excel file in sheet 2 of macro book - you can find sheet names in Sheet2 starting from A2 cell.
mainWorkBook.Sheets("Sheet2").Range("A" & sheetCount + 1).Value = workbook1.Sheets(sheetCount).Name
Next sheetCount
exit1:
workbook1.Close (False)
End Sub
In the Sheet1 (of macro workbook), enter the excel path (whose sheetnames you want) in cell A2.
Click the Active X button (keeping design mode disabled).
You can find the result in Sheet2 of the macro workbook (Add suitable column names in first row of Sheet1 and Sheet2 of macro workbook for readability, if needed).
i can do simple row deletions etc in single sheets in VBA, but this one has me stumped.
I need to search for ANY data (likely to be an email address, but in reality could be any text or number) in N1:N100, across all open worksheets...except Sheet1 (i.e. from Sheet2 to whatever the last open worksheet is)...
...if any data is found in the N1:N100 range for that particular worksheet (i.e. Sheet2) then do nothing and search the next worksheet (i.e. Sheet3)...if no data is found then enter "NONE" in cell N1 (on i.e. Sheet2) and then move onto the next worksheet (i.e. Sheet3).
Ive seen IF/ELSEIF/THEN code from other people, but it all seems to be sheet specific...and as i said im a bit out of my depth with this particular part.
This will feed into a larger bit of VBA code that ive got thats already spread over several sheets of A4, thought i had worked out all the bugs by now :D
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
' 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
' Insert your code here.
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
End Sub
Or you can loop over all of the worksheets using a For Each loop:
Sub WorksheetLoop2()
' Declare Current as a worksheet object variable.
Dim Current As Worksheet
' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
' Insert your code here.
' This line displays the worksheet name in a message box.
MsgBox Current.Name
Next
End Sub
I want to copy cells A103 to Y148 from first sheet to every sheet in my excel workbook. What code I should use?
Here is a four-step approach.
1. Design the action in plain words, like,
' loop through rows 103 to 148 in Sheet1
' copy the row
' loop through all other sheets in the workbook
' paste the row in the first empty row at the bottom of each
Research code for each action. For example, google for "VBA Excel loop through rows"
Be amazed at the multitude of ready-to-use code you will find, and at how quickly you will learn to do impossible things.
Come back here with any problems you can't solve on your own.
Depending on what you mean by 'every sheet in my excel workbook', this may be faster than looping a paste operation through all of the remaining worksheets.
Sub galumph()
Dim w As Long, wss As String, wsa as Variant
For w = 2 To Worksheets.Count
wss = wss & IIf(CBool(Len(wss)), ";", vbNullString) & Worksheets(w).Name
Next w
wsa = Split(wss, ";") 'make array of worksheet names
Worksheets(1).Range("A103:Y148").Copy 'copy from A103:Y148 on Sheet1
Worksheets(wsa).Select 'select all of the remaining worksheets
Worksheets(wsa(0)).Range("A1").Activate 'activate the destination on one of the worksheets
Worksheets(wsa(0)).Paste 'paste the data into all worksheets
End Sub
Is it possible to let Excel automatically select the first empty cell in column A, whenever I open the document?
I have got the following to find the the first empty line:
ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Value + 1.Count, 1).End(xlUp).Value + 1
In order to get Excel to select the first empty cell in column A when you open your workbook, you need to place the following code in the ThisWorkbook module.
Private Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
End Sub
This will select the first empty cell in the ActiveSheet. If you got multiple sheets in your workbook and you want to select the first empty row in a specific sheet, say Sheet1, you should change the second line of code to:
Set ws = ActiveWorkbook.Sheets("Sheet1")
You can do that.
You need write VBA(macro) program to realize.
Code you need is as follow
Private Sub Workbook_Open()
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select
End Sub
Meaning of code is:
"Private Sub Workbook_Open()" is predefined name subroutine which will be executed when the workbook be opened.
"ActiveWindow.Range("A65536").End(xlUp)" will find last cell with data in A column ("last cell")
"ActiveWindow.Range("A65536").End(xlUp).Offset(1,0)" will move to cell next to "last cell", that will be first blank cell.
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select will select tha first blank cell.
I assumed that you use Excel 2003 or older, OR number of rows with data in your worksheet is less than 65536.
If you use Excel 2007 or newer and you have rows with data in your worksheet more than 65536, please modify 65536 to the value large enough to cover rows in your worksheet.