Activate range of an inactive workbook - vba

In the following code:
Dim FirstSourceCell As Range
Set FirstSourceCell = Workbooks("Source.xlsx").Worksheets ("Settlements").Range("M9")
FirstSourceCell.Activate
why does the FirstSourceCell.Activate works only when the Source.xlsx workbook is activated?

You can only activate a cell in the currently active worksheet. So if the workbook isn't active, the worksheet can't be active, therefore you will get an error if you try to activate a cell on that inactive worksheet.
But there are very few cases where a cell needs to be activated. About the only real case is where you want to return control to the user with a specific cell selected. In that case, it is better to use Application.GoTo FirstSourceCell.

Related

How do I change specific worksheet to current worksheet?

I've a command button with a macro attached in workbook A that copies cells A2:B2 from any workbook in a folder and pastes them starting at cell B2 in the AMT worksheet also in workbook A.
I'd like to place a command button on every worksheet and Instead of always pasting to the AMT worksheet I'd like to paste to whichever worksheet the command button was clicked on.
This is how I've been transferring the data from one sheet to another so far but I'm not sure how to change AMT to worksheet macro was clicked on I've been warned about using .Select and .Activate but I'm not sure how else to do this, thanks.
'Transfer cells B2 & C2 from the results worksheet
With ws.Range("A2:B2")
ThisWorkbook.Worksheets("AMT").Range("B4").Offset(wbc, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With
If you don't want to do active sheet, you could set some cell to change, for example from a 0 to 1 after the click and then loop through each sheet to see which range was changed to pull the value you want.

Excel 2013 VBA: Setting activeworkbook when clicking between workbooks

I have a workbook with a userform that contains a listbox that is used to populate data on a sheet.
If I have multiple workbooks open and I click from one workbook directly to the listbox on the userform in the other workbook, the ListBox_Change event fires before Activeworkbook changes to reflect the workbook that contains the userform. So when the code reaches Set EqDataSht = ActiveWorkbook.Worksheets("Equipment-Data") I get a subscript out of range error because the workbook I'm coming from doesn't contain a sheet named "Equipment-Data".
What is the best way to set the ActiveWorkbook to the parent of the userform? Thoughts I've had are setting a public variable wb = ActiveWorkbook on workbook_open or just trapping Err.Number=9 and telling the user to click on the sheet before clicking the userform. I'm sure there is something simple I am completely overlooking (VBA amateur).
Thoughts?
Instead of activeworkbook use thisworkbook which returns the workbook in which the code resides.
To make it active thisworkbook.activate should work

VBA code for protecing range of cells (user intervention)

I want VBA code for protecting a range of cells(manually selected by user) in excel and rest of the cells will be locked. Ideally, I don't need to restrict the code for a specific range, it should allow a user to select a range of cell and lock those cells.
Please help me.
In Change of a the worksheet, lock the cells you selected and then, protect the worksheet.
something like :
with thisworkbook.worksheets("the name of the worksheet")
.unprotect
Target.locked=true
.protect
end with

Excel VBA Allow user to select an already open workbook and attach variables to selected workbook

With Excel 2013 VBA, I am familiar with setting the value of a Workbook variable with:
Set oWBSource = Workbooks.Open(strFileToOpen) 'Works well.
In my situation though, sometimes the Workbook is already open on the desktop. I am trying to find a more elegant way to "select" that spreadsheet and then attach my variable to that workbook to operate on. The code is running on a separate "master" spreadsheet.
Currently, I am asking the user if the file is already open. If so, then:
Dim rng As Range
Set rng = Application.InputBox("Select any cell on workbook to operate on.", "Click Workbook Cell", Type:=8) 'Stops code and allows user to select a cell on an open spreadsheet
Dim sTest As String 'variable used to test.
sTest = ActiveCell.Value 'Testing to see what value code is seeing.
sTest = ActiveWorkbook.Name 'Testing to see what value code is seeing
Set oWBSource = Workbooks(ActiveWorkbook.Name) 'Works if selected Workbook is maximized and minimized.
If I run the above code and when it runs the inputbox, if I click one cell on the spreadsheet I want the code to operate on and allow the code to continue, the variables still reference the Workbook where the code resides rather than the "selected" workbook. If, while selecting one cell, I also maximize and minimize the spreadsheet (basically Activate it) and then click a cell and allow the code to continue, the active spreadsheet is set and it seems to work. I realize the cell reference is doing nothing. But the pause allows me to "Activate" the desired spreadsheet.
I am looking for a more elegant way to handle attaching my code to an already open spreadsheet. My current solution is clunky. Isn't there a way to have the user select the open spreadsheet and then have my code "act" on that spreadsheet?
I know this is a weird question. Be kind rating it please??!!
If I understand correctly what you're trying to do, just use the rng to resolve the selected Workbook:
Set oWBSource = rng.Parent.Parent
rng.Parent is the Worksheet a Range is a part of, and the .Parent of a Worksheet is its Workbook.

Can I set a cell in a sheet as the selected one, without activating the sheet?

In a workbook I am creating I have created a procedure which loops through several worksheets clearing out some ranges++, preparing the workbook for a new year. However, I wanted to set a given cell in the sheet, e.g. F5 as the active one before moving on to the next sheet, as that's where it's most likely the users will start inputting data when they move to the sheet.
However, trying to put both .Select and .Activate into line 5 of the code below fails with Select / Activate method of Range class failed.
For Each ws In ThisWorkbook.Worksheets
If InStr(1, ws.CodeName, "Veke_", vbTextCompare) > 0 And Len(ws.CodeName) = 7 Then
Call lås_opp_ark(ws)
Call oppdater_ark_for_nytt_år(ws, førsteSkiftIVeka(dato))
ws.Range("F5").Select
Call lås_ark(ws)
End If
Next ws
Trying to figure out why this didn't work I eventually came across this documentation-page from Microsoft which states that:
Before you can use the Selection property successfully, you must activate a workbook, activate or select a sheet, and then select a range (or other object) using the Select method.
Which I take to mean that using Select on anything but the active worksheet won't work. The page doesn't explicitly state anything about Activate, but I assume the reasons for the errors I'm getting to be the same.
Now, my question is this - is there any way to set the active cell in a nonactive worksheet without activating it first? If not, would activating each sheet in the workbook to set the active cell be very resource intensive? I guess I could check the latter myself, but any input would be interesting too.
No need to select the cell, instead activate it. Try this:
Replace:
ws.Range("F5").Select
with this:
Application.Goto ws.Cells(1), 1 'This activates ws and scrolls to place cell `A1` at the top of the window
Application.Goto ws.Range("F5") 'This selects Cell `F5`
see Application.Goto Method (Excel)