Using the contents of cell's as part of a file location in a macro - vba

This is what I have:
Option Explicit
Sub EditWorksheet()
Workbooks.Open "Y:\Laurence\contents(J2)"
Sheets("contents(N2)").Select
End Sub
So what I want to do is have VBA call on the values of cells J2 and N2 in order to decide which workbook and worksheet to open. For your information J2 could contain for example "Forecast2016.xlsx" and N2 may contain "MASTER". Just to be clear, for every possible content these cells can have, I do indeed have a spreadsheet in the relevant file location (Y:\Laurence).
Thanks in advance!

The problem is you are not passing a string, but a range with that code. This causes the run time error 13. Instead of
Workbooks.Open "Y:\Laurence\contents(J2)"
Sheets("contents(N2)").Select
try
Workbooks.Open "Y:\Laurence\" & Sheets("Contents").Range("N2").Value
Sheets(Sheets("Contents").Range("N2").Value).Select
This forces the value of the cell to be passed, not a reference to the range. Also notice I qualified your worksheet that you were referencing as well. As you had it before, it would have pulled Range("N2") from whatever sheet happened to be open at that moment.

Related

Use cell value to activate another sheet then clear original cell value without an error

I have cell A1 on a sheet named "Data" that looks for input which when received, looks for a sheet name that matches it. If it finds a match, it opens that sheet. I'm trying to clear the value entered in A1 of "Data" after that second sheet has been opened however I'm getting a runtime error that appears to still be looking for the data used to open the second sheet. Here is the code for the "Data" sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
Sheets(Target.Value).Activate
End Sub
I tried using a variable in place of Target.Value hoping that after deleting the actual data in A1, the variable would satisfy the code.
I've ran the following line on at the end of the block above as well as on the sheet which gets activated but either way I get an error.
Sheets("Data").Range("A1:A1").ClearContents
The error is "Run-time error 9 Subscript out of range.
How do I clear the contents of A1 on "Data" so it's ready to receive another request to open another sheet when I'm done with the last one?
This is the correct syntax for a single cell:
Sheets("Data").Range("A1").ClearContents
...however using a range of A1:A1 should also work, so if you're getting Subscript Out Of Range then there is likely no worksheet named Data.
If you have multiple workbooks open then by default, it's looking for the Active Workbook (whose name in contained in ActiveWorkbook.Name.) So if you're going to be working with multiple workbooks, you should explicitly state the workbook name as well:
Workbooks("Book1").Sheets("Sheet1").Range("A1").ClearContents
I didn't need to clear the value after all. I added a couple lines to ensure when returning to the "Data" sheet, cell A1 was selected. This allows for a new value to be entered replacing the previous value and serves the purpose.
Just a slight change to your code. "Is Nothing" does not work. The below code works fine for me.
If Intersect(Target, Range("A1")) = "" Then Exit Sub
Sheets(Target.Value).Activate
Target.ClearContents

Copy a static range on one sheet, and paste in a dynamic range in another sheet based on a single value in a cell

I have three parts to this problem. I have a single cell with a Week number in Sheet1!A1. I have a static range in Sheet1!B1:F1 that needs to be copied. Then I need to paste the value in a dynamic range in Sheet2 offset by the week number for rows. This is part of a larger macro I am writing for a sheet I use regularly, but I seem to have those parts down. I may be either oversimplifying or oversimplifying but this is what I have currently.
Sub CopyPaste()
Sheets(1).Range("B1:F1").Copy
OffsetRange = Sheets(1).Cells(1,1).Value
Sheets(2).Cells(1+OffsetRange,1).Paste
End Sub
When I run this, it either gives me a Runtime Error 9 or Runtime Error 438.
Anyone know whats causing these errors? When I paste the range, does the cells object point towards the first cell of the copied range when I paste in at the location?
Try it as,
Option Explicit
Sub CopyPasteOffset()
Dim OffsetRange As Long
OffsetRange = Worksheets(1).Cells(1, 1).Value
Worksheets(1).Range("B1:F1").Copy _
Destination:=Worksheets(2).Cells(1 + OffsetRange, 1)
End Sub
The .Paste method is a member of Worksheet, not Range or Cells. You may have it confused with .PasteSpecial which is a member of the Range object. In any event, it is unnecessary as a destination can be applied directly to the copy command.

Range SpecialCells ClearContents clears whole sheet instead

I have a sheet in Excel 2010 which is setup as a pseudo form (I didn't create it, I'm just trying to fix it) so formatting suggests that the user can only enter in certain cells. Depending on certain functionality these areas need to be reset, i.e. cleared although formulae and standard/conditional formatting need to be kept. I have defined each of these cells/ranges as named ranges so I can easily loop through them using the following code: -
Public Sub ResetDetailSheet()
Dim nm As Name
With ThisWorkbook
For Each nm In .Names
If Left(nm.Name, 9) = "nmrDetail" Then
Range(nm.Name).SpecialCells(xlCellTypeConstants).ClearContents
End If
Next
End With
End Sub
For some reason instead of clearing the constants from the specific range it is clearing constants from the entire sheet so I am losing all titles/headings. Formulae and standard/conditional formatting are staying as expected.
What am I doing wrong?!?!
As a test using the immediate window I tried clearing a specific cell, e.g.
Range("G7").SpecialCells(xlCellTypeConstants).ClearContents
But this still cleared all constants from the entire sheet.
What am I missing? I don't understand. Maybe I'm being dumb.
Sorry, I can't upload an example. This place is pretty locked down.
Range({any single cell}).SpecialCells({whatever}) seems to work off the entire sheet.
Range({more than one cell}).SpecialCells({whatever}) seems to work off the specified cells.
So, make sure your range has more than a single cell before you clear it - if the range is only a single cell, then check if it .HasFormula; if that's the case then its .Value isn't a constant:
With ThisWorkbook
For Each nm In .Names
If Left(nm.Name, 9) = "nmrDetail" Then
If nm.RefersToRange.Count > 1 Then
nm.RefersToRange.SpecialCells(xlCellTypeConstants).ClearContents
ElseIf Not nm.RefersToRange.HasFormula Then
nm.RefersToRange.ClearContents
End If
End If
Next
End With
Note that I'm using Name.RefersToRange instead of fetching the range by name off the active sheet.

Excel worksheet name

I want to make a macro that copies data from one sheet to another.
No problem, but I named the sheet with an emoji.
How can I tell VBA which sheet he has to use if the name of the sheet is for example: 🏠
Thanks in advance
You can use the AscW function to find the appropriate unicode value for the item and use that to find the correct sheet
To find the name of any given worksheet, activate the worksheet, open the VBA Editor (Alt+F11), open the Immediate Window (Ctrl+G) and type the following:
? ActiveSheet.Name
If the name for some reason is nonsensical, and only consists of a single character, run ? Asc(ActiveSheet.Name) to get the ASCII value.
Referencing an ASCII value in VBA can be done by calling Chr(putValueHere), for example like this:
Worksheets(1).Name = Chr(50)
If you have a plethora of sheets, you can print all of them by running this code:
Sub SheetNamePrinter
For i = 1 To Worksheets.Count
Debug.Print Worksheets(i).Name
Next i
End Sub

Excel macro to save only values of a file with different file names

I wish to save my current excel file as a different file with file name as the value in the cell B10 and taking only values (not formulas) from the current file.
I got the code for getting only values from this link: Saving values from a workbook
Any help appreciated. Thanks.
EDIT:
I want to make a kind of template which will help my colleagues to document in a better way, without wasting disk space in formulas, which takes up about 1.7 mb per *.xls file. If I save only values, it takes about 600 kb. I want it to be flexible to different users, people who won't have to use long instructions to do this.
* vba isn't my area of expertise, and I haven't written any code for this work, so basically any possible way is welcome.
This code will do the need
Sub Macro1()
Dim x As String
Dim y As String
x = "C:\" 'path to file
y = Range("C1").Value 'Reference of the cell which contains the value
Z = x + y
Z = Z + ".xlsm" 'Format of macro enabled worksheet
ActiveWorkbook.SaveAs Filename:=Z, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Since you provided a Text only question, I should give you a Text Only Answer.
To do what you want you have to:
Copy your worksheet using Sheets Object Copy method. It has optional arguments and if you omit it, it automatically creates a new workbook that contains the copied worksheet.
After copying, you can use ActiveObject (e.g. ActiveWorkbook, ActiveSheet etc.) to make reference to your newly created workbook and worksheet.
Once you have referenced the objects properly, you can now use Cells property to select all ranges and copy. Then use Range PasteSpecial Method to convert formulas to values.
Lastly you'll need to save the workbook using the Workbook Object SaveAs Method.
Hope this somehow leads you to a solution. HTH.