What I want to do is to replace all cell values in the multiple worksheets except one.
So For example, there are worksheets sheet1, sheet2, sheet3, and I want to replace all the values "M9" into "M8", but leave M9 in Sheet1 remains unchanged.
This is my code:
`For Each Worksheet in WorkSheets
If Worksheet <> ThisWorkbook.Worksheets("Sheet1") Then
Worksheet.Cells.Replace .....
End if
Next`
For For-Next, Just ignore the details as it is related to my contents. I thought this logic would have been right, but vba keep on coming up with error.
Do you know why? And are there any right way to replace the values excluding Sheet1?
Thanks for your help
If you want clearing all cells content, as I suppose, please use the next way. It also shows the way of replacing:
Sub RemoveCellsValue()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
ws.cells.ClearContents
'or
'ws.Cells.Replace "Oldstring","NewString" 'if you need replacing
End If
Next
End Sub
If replacing, the function needs some more parameters. Especially to state if you need to replace if the whole string is found, or only part of it.
Related
I am trying to unhide a group of worksheets if they meet a certain condition. This uses a user form, triggered by a command button, with selection options and another command button. The expected behavior is that once the selection has been made and the button has been pressed, all worksheets meeting the criteria will be unhidden. The Target word is present at different locations along the first row and all cells before it are empty on that row. Ideally, the process will scan each cell in the first row of each worksheet in the workbook until it comes across the Target, unhide the worksheet, then move on to the next worksheet to start the process over again until all worksheets with the workbook have been checked.
Upon activation of the command button on the user form I have the following:
Private Sub ContinueCommand_Click()
Dim Valid As Boolean, wks As Worksheet, c As Integer, actCol As Long
actCol = ActiveSheet.Range("A1").End(xlToRight).Column
For Each wks In ActiveWorkbook.Worksheets
For c = 1 To actCol
If ActiveCell.Value = "Target" Then
wks.Visible = xlSheetVisible
Exit For
End If
Next c
Next wks
Valid = True
If Valid = True Then
Unload Me
End If
End Sub
I've borrowed from several sources, including here for using ActiveCell, determining if a value exists, unhidding worksheets, Finding values within a range, and searching for a string. Any help would be greatly appreciated.
As I said in my comments there are some issues with the way you've chosen to implement this.
Your For c = 1 To actCol loop is not needed. This can be easily seen because c is not really used anywhere in the loop.
Let's assume your Target value is in wks.Range("A100") (the 1st row and 100th column).
Your code would then perform the exact same operation 100 times and would come up with the exact same result. That's what leads you to use Exit For, which is a bad practice.
If I understood your initial post correctly, if Target exists in a particular worksheet, then all cells before Target are empty.
If that's the case, the Target will either be in wks.Range("A1") or in wks.Range("A1").End(xlToRight). If it's not in either of these two cells then it doesn't exist at all in this particular worksheet, which would mean that the 1st row is completely empty. You don't need to check any more cells apart from these two.
Your code does not check whether Target is in wks.Range("A1").
Also your use of Application.Match, makes me believe that you have probably been misled by the common misconception that wks.Range("A1").End(xlToRight) is a range of cells starting from A1 and extending all the way to the last non-empty cell in the 1st row.
The truth is that wks.Range("A1").End(xlToRight) is a single cell rather than a range of cells. Selecting A1 and then pressing CTRL+right arrow, will show you exactly which cell it is.
I might be missing something, but according to your description in the initial post, I would do something like the following:
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
If sht.Range("A1").Value = "Target" Or sht.Range("A1").End(xlToRight).Value = "Target" Then
sht.Visible = xlSheetVisible
Else
MsgBox "target was not found in " & sht.Name
End If
Next sht
I want to thank BruceWayne, Scott Craner, Stavros Jon, Darell H whom all helped me get closer to this answer. The final result looked like this:
Private Sub ContinueCommand_Click()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
If Not IsError(Application.Match("Target", wks.Range("A1").End(xlToRight), 0)) Then
wks.Visible = xlSheetVisible
End If
Next wks
Unload Me
End Sub
If anyone in the future has issues getting this to work, let me know and I will post a more complete version.
I have the following simple Excel spreadsheet:
A
1 Sheet1 =MID(CELL("filename",Sheet1!K1),FIND("]",CELL("filename",Sheet1!K1))+1,255)
2 Sheet2 =MID(CELL("filename",Sheet2!K1),FIND("]",CELL("filename",Sheet2!K1))+1,255)
3 Sheet3 =MID(CELL("filename",Sheet3!K1),FIND("]",CELL("filename",Sheet3!K1))+1,255)
4 Sheet4 =MID(CELL("filename",Sheet4!K1),FIND("]",CELL("filename",Sheet4!K1))+1,255)
5 Sheet5 =MID(CELL("filename",Sheet5!K1),FIND("]",CELL("filename",Sheet5!K1))+1,255)
6
7
In Column A there is a list of all sheets in the Excel file. I list the sheets using the formula that you can see next to it.
All this works fine so far.
Now it can happen that some sheets in my spreadsheet are invisible (hidden). In this case I want that those sheets do not appear in the list above. Therefore, I wonder if there is a formula that can identify if a sheet is visible or not. Something like this:
IF MID(CELL("filename",Sheet1!K1),FIND("]",CELL("filename",Sheet1!K1))+1,255) = Invisible THEN ""
Do you have any idea how to solve this issue?
I don't really understand the purpose of this list... it seems like you're just duplicating the list of worksheet "tabs" shown at the bottom of the screen.
I sense something being made more complicated than necessary; either an XY Problem, or some duplicate data that you're trying to manage without reorganizing it. :-)
Regardless, based on my understanding of your question, there are no built-in functions to do what you need, but these VBA examples should give you some ideas:
This procedure lists all visible worksheets, in the Immediate Window (Hit Ctrl+G from VBA to view it):
Sub ListSheets() 'list in immediate window
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Visible = xlSheetVisible Then Debug.Print sht.Name
Next sht
End Sub
This procedure is similar excep lists them on the active worksheet, leaving blanks for hidden sheets (like your example would imply):
Sub ListSheets() 'list on worksheet
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Visible = xlSheetVisible Then Range("A" & sht.Index) = sht.Name
Next sht
End Sub
This function can be called from a worksheet formula and lists the name of the worksheet you specify by index number. It returns "" (empty string/nothing) if the worksheet is not set to Visible, or if it doesn't exist.
Function listSheet(shtNum As Long) As String
On Error Resume Next
If Sheets(shtNum).Visible = xlSheetVisible Then listSheet = Sheets(shtNum).Name
End Function
Related note:
There are three types of worksheet visibility.
More Info:
MSDN: Worksheet.Visible Property (Excel)
I have an excel workbook that contains multiple sheets. In this workbook sheets occasionally are removed, renamed, or added. This workbook features a sheet (Sheet1) that is supposed to be used as a command center for printing copies of the other sheets in the workbook. Unfortunately, this printing command center does not work as intended.
The command center has only two columns. In column A is a list with the names of the other Sheets. In column B the user can specify how many copies to print of the respective sheet in column A.
This is the current code:
Sub PrintSheets()
Dim mysheets As Range
For Each mySheets In Sheet1.Range("A2:A100")
If mySheets.Offset(0, 1).Value <> "" Then Sheets(mySheets.Value).PrintOut Copies:=mySheets.Offset(0, 1).Value
Next mySheets
End Sub
The first two sheets are printed as intended and then I get a "Run-time error '9': Subscript out of range"
1) How can I fix the error?
2) Can the list with the sheet names be generated automatically and sorted by their position (i.e., left to right in the sheet tab -> top to bottom in the column)?
If you rename, add and remove sheets, you will have to update your list before printing. This sub will clear Range("A2:A100") and it will insert all sheets names in your first sheet. Make sure your "command center" sheet is the first one (or change the index reference to the name of that special sheet).
Sub LIST_SHEETS()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim i As Byte
ThisWorkbook.Sheets(1).Range("A2:A100").Clear
i = 2
For Each ws In ThisWorkbook.Sheets
ThisWorkbook.Sheets(1).Range("A" & i).Value = ws.Name
i = i + 1
Next ws
Application.ScreenUpdating = True
End Sub
I'm afraid you will have to figure out where to put the code. I don't know your needs, so maybe after renaming/adding/deleting all sheets, or maybe when Workbook opens or something like that. Try it!.
It appears that a name in the list of sheets, does not exactly match the actual name. You can use either of the two options below, to loop through each sheet
For Each sht in ThisWorkbook.Sheets
'Do Something
Next
Or
Dim sht as Worksheet
For i= 0 to ThisWorkbook.Sheets.Count
Set sht=ThisWorkbook.Sheets(i)
'Do something
Next
The second option is not reliable when the sheets are deleted (their integer index may not be sequential then) so I would prefer the first option.
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.
I have several Named Ranges that contain constant data in one worksheet.
I have a target range where one of the Named Ranges will be copied to, on another worksheet.
The Named Range that will be copied is selected based on user input into other cells.
I have managed to create the name of the relevant Named Range in a single cell.
What I can't do (as I'm a VBA Noob who thought he could do all this without using VBA!), is create a Macro that reads the relevant cell, and then copies whatever Name Range it reads, into the target range.
Any assistance most humbly and gratefully accepted.
Let's say, the name of your range is MyRange
So to copy the range you have to do this
Range("MyRange").Copy
Now let's assume that Cell A1 of Sheet1 has the word MyRange. In such a scenario, you can retrieve the value of the cell A1 using Range("A1").Value
So
Range("MyRange").Copy
becomes
Range(Range("A1").Value).Copy
Here is a complete example. I am assuming that you want to copy to say Cell A1 of Sheet2
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
wsI.Range(wsI.Range("A1").Value).Copy wsO.Range("A1")
End Sub
i am not sure if thats what you need, but, if you need just to copy the content of the A1 cell from sheet1 to sheet2 for example, just do this:
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
you may wnat to encapsulate the code into a sub as well:
Sub copyvalues()
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
End Sub
...and finally, you must to insert a button, using onclick() event to fire the sub(which you must to allocate into a module)
sorry my bad english, hope it helps you.
Public Function copyNamevalues(srcName As Name, destName As Name)
srcName.RefersToRange.Cells.Copy (destName.RefersToRange.Cells)
End Function