Create copy of excell sheet with code problems. - excel

Created a sheet in excel in which when new information is needed I will simply create a copy of the worksheet into a new tab. However I have a code that allows me to clear certain cells or erase them to enter the data, it goes as:
Sub Clear_Cells()
Range("B5").ClearContents
Range("C5").ClearContents
Range("D5").ClearContents
Range("E5").ClearContents
Range("F5").ClearContents
Range("B8").ClearContents
Range("C8").ClearContents
Range("E8").ClearContents
Range("C9").ClearContents
Range("C13").ClearContents
Range("C14").ClearContents
Range("D13").ClearContents
Range("D14").ClearContents
Range("E15").ClearContents
Range("B18").ClearContents
Range("C18").ClearContents
Range("D18").ClearContents
Range("E18").ClearContents
Range("F18").ClearContents
End Sub
The problem is when I create a new page and try to clear the cells in that page it always clears the cells of the original page. I know it has to be something simple but just cannot put finger on it.

Related

How do I copy a range including hidden cells(by filter)?

I know basically the same questions has previosly been asked and answered on many forums, including stack overflow, but none of the answers satisfy my requirements.
I want to press a button, which will copy a range of around 100 cells. I then want to press a different button, in a totally different workbook (which is in no way connected to the first workbook) and that will paste my copied range.
The issue is that I have a filter in my workbook, which will hide some of the cells in the range. These are not being copied but I need to copy the full range. (The reason I want to copy the full range is because the values need to align when I paste it)
My issue with all other given solutions are:
One solution is to remove the filter when I copy the range. This is not something I want to do as I don't know a way to restore the filter. If this is done before I paste the values, the copied range will "exit?" (it will no longer be copied). And due to the files not being connected, I can't perform any actions with the paste button.
Using a loop to copy the range as a array(Variant), This doesn't work since I can't "transfer" that variant to another workbook which is not connected. Or at least I don't know how to do that.
Is there any other method I can use?
It is a bit of a hack, but because both workbooks have access to the set of custom lists defined for Excel, you could create a custom list with the information needed to identify the source range from the target workbook. The code for the copy button would record the range address, sheet name, and workbook name of the selected range as follows:
Sub copy_range_info()
Application.AddCustomList Array("DeleteMe", Selection.Address, Selection.Parent.Name, Selection.Parent.Parent.Name)
End Sub
On the destination worksheet, the code to paste data would look like this:
Sub paste_range_from_other_workbook()
Dim last_list As Variant
last_list = Application.GetCustomListContents(Application.CustomListCount)
If last_list(1) = "DeleteMe" Then
Workbooks(last_list(4)).Worksheets(last_list(3)).Range(last_list(2)).Copy
ActiveCell.PasteSpecial xlPasteValues
Application.DeleteCustomList Application.CustomListCount
Else
MsgBox "You need to copy a range first using that special button"
End If
End Sub
Because this creates a custom list in Excel that will be permanent, I'm deleting it just before the "else" in the code above. It might be advisable to scan the custom list and delete any lists that begin with "DeleteMe" so if someone does a copy without a corresponding paste, it won't result in more than one custom list of this type.

Make variable hyperlink from cell data

Hi I am kind of new to VBA and i can't seem to find what i am looking for.
What i want to make is a macro that links to another page in my workbook that refers to data in a certain cell.
from certain datapoints i have a cell set up that as everything is filled in it gives the name of the page i want to link to (lets say "overview_Oct_2020" by filling in the month and year in other cells), and when running the macro go to that.
I seem to totally blank on how to do it. I made a =HYPERLINK() version pretty easily, but i want to change that to a button, hence the reason for a macro.
so technically i want to make a button with a macro that goes to [TEXT IN CEL A1]!A1
Put this macro in a standard module and assign it to a button on the worksheet
Sub link()
Dim textInCelA1 as String
textInCelA1 = Range("A1").value
Sheets(textInCelA1).Activate
Range("A1").Select
End Sub

Need to copy cell to another sheet and then overwrite with new value (loop)

I need to make a VBA which copy a cell into a cell in another sheet. (Same workbook).
Afterwards i have to create a pdf (i have VBA for this).
After the pdf creation, i need do this all over automatically and i would like to have a VBA for this. Because i need to make 200 pdfs
My question is therefore:
How can i create a VBA which copy a cell from a list i sheetB to a cell in SheetA, and after the creation of the pdf, do it all over with the next cellvalue from sheetB? I guess it is something with the "looping" tool?
A good way to create basic VBA to automate standard tasks it to record a macro while you perform the steps. Afterwards, you can view the VBA that was generated for the macro, automate it further as necessary. Here's some instruction and examples:
Getting Started with VBA in Excel 2010
Recording a Macro to Generate Code
Revising Recorded Visual Basic Macros
I'm not aware of a "looping tool" but there are various ways to loop through data with Excel.
Edit:
Looping through a list is the easy part. For example, the following code pops up a MsgBox with the value of each cell in range A1 to A15:
Dim cel As Range
For Each cel In Sheets("Sheet1").Range("A1:A15")
MsgBox cel.Value
Next cel
Get your VBA together for doing your task once, add the code to your question, and then we can stick the loop in the correct spot.

Remove error indicating unlocked cells are protected with VBA

I have a template sheet that is copied through VBA into a new sheet.
The new sheet is added by clicking on a button that makes a userform pop up, and the information in the userform is transferred to the new sheet.
This new sheet contains locked headline cells as well as unlocked empty cells for the user to put information into. The sheet is protected but allows users to select locked and unlocked cells.
Everything worked for some months until the other day. When a new sheet is created and the user tries to fill in the unlocked cells, Excel responds as if they were locked!
The following information pops up:
"The cell or chart you're trying to change is on a protected sheet. To make changes, click Unprotect Sheet in the Review tab (you might need a password)."
This error disappears after the user does any of the following
1) double click in any empty cell (instead of just selecting it) and put in information this way and then click enter, then all cells behave normally.
2) activate any other worksheet and then return to this sheet
3) choose Format>Unprotect Sheet (this does not unprotect the sheet but it removes the error. If the user wants to unprotect the sheet then they have to click Format>Unprotect Sheet twice, i.e. first to remove this weird error and then to unprotect it).
I am trying, with VBA, to remove this error by doing any of 1, 2 or 3 above.
Any ideas how to solve this?
An additional problem is it is not possible to scroll with the mouse in the sheet while having the problem.
I tried the following code:
ActiveSheet.EnableOutlining = True
ActiveSheet.Protect Contents:=True, userInterfaceOnly:=True
After some googling I managed to solve this by doing the following:
Create a Module in VBA and add the following code:
Public Sub RefreshActiveCellSelection()
ActiveCell.Select
End Sub
Whenever you perform a Workbook.Activate or Worksheet.Activate, immediately call
Application.OnTime Now, "RefreshActiveCellSelection"
Another solution was to uninstall KB3085502
I found the answer here: http://answers.microsoft.com/en-us/office/forum/office_2013_release-office_install/microsoft-update-excel-2013-kb3085502-32-bit/0cc7d498-92cb-4478-9554-77cff286c847

Waiting for user input - with DoEvents a good idea?

My macro is going to compare a sheet with another sheet. This second sheet needs the user to paste data in there. (Note: The data being copied is not in Excel).
One way is to run the macro, and end it by prompting the user to paste the data in, then run "Macro2". However, I'd like to keep it all in one macro, so have found a way to wait for user input before continuing. This seems to work for me, so my main question is:
How stable is doing it this way?
...macro stuff above here
MsgBox ("[Please copy the data into the new sheet, after clicking 'OK']")
Do While WorksheetFunction.CountA(newWS.Cells(1, 7)) < 1
DoEvents
Loop
...then after the user pastes info, continue on, using the data that's been pasted.
The idea is that DoEvents just runs and runs while my sheet is blank. Right after the user pastes the data into the newWS, the macro continues on (since it will see data in column 7)...
Is this an okay method, or is it a bad idea to use like that? I've never really used DoEvents, so don't know if it's doing something in the background that could cause issues.
Edit: The data is in Lotus Notes, which I can export to Excel. However, that takes a few more steps (and I'd rather not create some new temporary excel files), so copy/pasting is my preferred method. This question is half practical, and half theoretical. Sorry for any confusion!
Probably not the best idea. Instead, allow them to select the data and perform the copy, all through VBA:
MsgBox ("[Please select data to copy into the new sheet, then press 'OK']")
newWs.Cells(1,1).PasteSpecial '### Modify to your specific location to paste the data
'Here you can add logic to validate that they have pasted enough data,
' and use control statement to prompt them to paste more data, etc.,
' if necessary, or exit sub early
'For example:
If WorksheetFunction.CountA(newWS.Cells(1, 7)) < 1 Then
MsgBox "Try again!"
Exit Sub
End If
Alternatively, you can use a DataObject:
Dim dataObj As New MSForms.DataObject
dataObj.GetFromClipboard
newWs.Cells(1,7).Value = dataObj.GetText
You could restructure the code so that it lives inside of a userform with ShowModal set to false (in the properties). Code prior to when you want the user to gather data can be put in the useform's initialize event. Then the userfrom shows (with a simple label caption and an okay button). Since it is modeless the user can copy data from an external program and paste it in. Then the rest of the code runs after the user hits okay. The form itself can be hidden during this phase. As proof of concept I created the following form:
with the following code:
Private Sub CommandButton1_Click()
Me.Hide
MsgBox Range("A1").Value
Unload Me
End Sub
Private Sub UserForm_Initialize()
'macro code can go here
'it runs before the form shows
'e.g.
MsgBox "Initializing"
End Sub
I launch the form on a blank sheet. First a message box appears before the code (confirming that code can run while the form is being initialized but before it is visible) then the form shows:
I go to an open instance of Notepad which contain a sentence and, while the form is still open -- paste it into A1:
Finally I press okay and the userform then hides itself but continues to run code (which now has access to the copied data):
Remember to unload the form at the end.

Resources