Clearing a worksheet using VBA - excel

Using VBA I am writing some code that adds text to another worksheet from drop down boxes. I would like to add a button that 'resets' the workbook so that the worksheet that text is being added to is cleared and picking from the drop boxes can be started again.
I have come across the clear contents and clear formats code.
Cells.ClearContents
Cells.ClearFormats
However, I am unsure how to use these on the worksheet from a certain point as the bottom of the worksheet depends on how much text was added.

Related

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

Drop down menu - data validation - looking for source

I have been wracking my brain and I’ve lost all will to work (yay!) I have a spreadsheet (wow!) with a drop-down menu and want to remove the drop down list – start new/fresh - however in the validation data tab there is no source to “clear all” and I cannot for the life of me find the main cell that’s picking up the list - but the list is there – just no link or there is one but it’s invisible.
I can’t even create a new column to create a new drop-down because for some reason it copies the same cells over into the new column automatically so I’m stuck with the same list without being able to change it.
Am I missing something?? I can’t see any hidden rows or columns. I also have a drop-down menu where I don’t even know or can’t even see the list. The spreadsheet is not linked to any other worksheet either and it was created a while back by a person who no longer works here, so can't ask them.
Please don’t tell me I have to create a whole new spreadsheet – there is way too much data in there and it’s giving me a headache. Any assistance would be greatly appreciated and would save me from going grey prematurely.
I'm using Windows 10 pro and Excel 2016.
Excel Method:
Data - Data Tools - Data Validation - Settings tab - Clear All.
VBA Method (One sheet):
Option Explicit
Sub Clear_Validation()
With ThisWorkbook.Worksheets("Sheet1")
.Cells.Validation.Delete
End With
End Sub
VBA Method (Loop sheets):
Option Explicit
Sub Clear_Validation()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.Cells.Validation.Delete
End With
Next ws
End Sub

Capture cell value on worksheet when user right clicks a tab and deletes?

Been searching but cannot find any reference to what I am trying to accomplish.
Background
I use a worksheet (worksheer#2) to enter certain data for an aircraft model. When the data is correct I run a macro to copy that data into another worksheet and rename the cipued one with the aircraft name. I do this for as many aircraft as i need. All created sheets are all placed between two other blank sheets named Start and End.
I can manipulate any specific worksheet by name using buttons on worksheet #2 perfectly and when the macro runs it updates relevant cells on worksheet#2 accordingly.
Those values are required elsewhere in my workbook and have to be correct. That all works fine but it all falls over when the worksheet is deleted/moved by way of right clicking the worksheet tab and then deleteing or holding and dragging the sheet to another location.
Is there a way to capture the worksheet name when it is manipulated in this fashion so I can use the info to update data on worksheet #2 with?
There is a worksheet-level event called BeforeDelete.
Right click on the tab and choose View Code
In the General drop-down at the top-left of the VBA Editor, choose ``WorkSheet" and in the (Declarations) drop down at top-right, choose BeforeDelete.
This will create an empty procedure where you can add your code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
More Information:
Worksheet.BeforeDelete Event (Excel)

Excel copy-paste including hidden columns

I'm using a Excel 2016 worksheet that has a couple of columns hidden for UI reasons. I need to be able to filter out data and then copy-paste it to another sheet with hidden columns intact and showing after pasting in the destination (it will contain a longer log of similar transactions, not just one copy-paste).
Adding a pic of the objective - i.e. hoping to have the hidden contents of columns B and C being pasted into the destination spreadsheet. Is this possible at all?
Probably not great form to ask 2 questions in one post, however are there alternatives to performing filtering and copy-paste function to another spreadsheet manually? I.e.:
run manual filter to clear blanks in Quantity field;
make a selection
do manual Ctrl+C - Ctrl+V function
Is there a way to make it easier? Unfortunately no VBA or macro experience as of yet.
Edit - Completely misunderstood the question!
You want to include hidden cells when you copy - that's standard behavior for hidden cells but not for filtered columns. If you want to avoid VBA abd you're dealing with small contiguous ranges then a simple formula may be the easiest solution.
Using your example, I will arbitrarily name the source worksheet "Sheet1" and the destination "Sheet2". In Sheet2, click in cell A2 and type this into the formula bar: =Sheet1!A3 Now click the bottom right corner of cell A2 and drag it to the right through D2 then down to D7.
With the range highlighted, press ctrl C to copy, then right click to paste special values.
You're done!
Here's a VBA solution:
Sub copyrng()
Dim srcrng As Range
Dim tmprng As Range
Dim dstrng As Range
Dim srcws As Worksheet
Dim dstws As Worksheet
Set srcrng = Application.InputBox("Area to copy", "Source", Type:=8)
Set srcws = srcrng.Parent
Set tmprng = Application.InputBox("Top Left Corner of Destination", "Destination", Type:=8)
Set dstws = tmprng.Parent
Set dstrng = dstws.Range(tmprng.Address, tmprng.Parent.Cells(tmprng.Row + srcrng.Rows.Count - 1, tmprng.Column + srcrng.Columns.Count - 1))
dstrng = srcrng.Value
End Sub
First answer (answered wrong question)
You can copy visible cells using "Go To..."
Highlight the range you want to copy, press Ctrl G, click "Special...", select "Visible Cells Only", and then press Ctrl C to copy.
Now all hidden cells will be left behind when you paste.
No, you cannot do this with regular Excel features since Excel cannot know which columns/cells to skip when one of the column have blank values, this is something has to be decided and done by a human.
Maybe this is a good time to enter the world of Macros, since you do not need a custom code but can use the recorded macro without any further manipulation. This Excel feature is for inexperienced users just like you.
View / Macros / Record Macro
Name your macro
Do what you need to, keeping in mind that Excel is recording your every move by converting them into VBA codes in the background. For your case, do the following:
Filter the blanks using filter combo-box
Select the range by using CTRL-G / Special / Current Region (do not select the cells by mouse or with your keyboard, your code should be generic should not contain manual ranges since you do not want to do any coding)
CTRL-C to copy
If "to-be-pasted" cell is not fixed for all your cases, then you should stop recording your macro here. If pasting cell is fixed then Paste the contents while the macro is recording.
After the macro is recorded, assign a shortcut to your new Macro using:
Macros / View Macros / Options menu
Voila! Now you are able to do exactly what you have done when recording your macro by using that keyboard shortcut. If you did not paste the content when recording then you s/b using your macro short cut and go to the cell you want to paste and press CTRL-V.
When you feel confident enough, try the Edit menu in the Macros and see what code you have in hand, maybe make some small changes etc. I saw many people who are not familiar with basic coding at the beginning but somehow started writing their own codes after seeing this feature in Excel. Good Luck!
ProfoundlyOblivious code is pretty cool but the
dstws = activesheet
will always be the source since the activesheet passes back straight after the inputbox.
I tried changing it to
Set dstws = tmprng.Parent
but for some reason this then breaks the
Set dstrng = dstws.Range..
I get a Run time error 1004 Method range of object _Worksheet failed?!?!
If I could fix that this solution would work for you with any destination, even other files.
The alternative is to use vba to un-filter the data, then do a copy, then put same filter(s) back on. Once that is done you can go anywhere and paste what is now on the clipboard.

How to remove or delete comments in excel worksheet?

As I click on the particular cells in excel, comments appears that disturb me much so I want vba code to delete all the comments instantly in the active worksheet.
All you really need to do is get a range, then clear comments:
Worksheets("MySheet").Activate
ActiveSheet.UsedRange.ClearComments
Does that help?
More Detail
To get the above code to work, there are several approaches. The one I recommend here is:
Open your Excel workbook.
Click the Visual Basic option on the Developer tab. This opens a VBA window with a tree control to the left, which shows the worksheets and workbooks.
Right-click the worksheet and select Insert Module.
In the module window that opens, paste the code I show at the bottom of these instructions.
Save the worksheet as type Excel Macro-Enabled Workbook.
Close the VBA window.
When back in Excel, hit to bring up the Run Macro window. You should see your RemoveComments macro listed. Now click Run and your comments should be removed.
I actually tested this, so it will work if done properly. If it still doesn't work for you, be sure that the worksheet in question is the first worksheet in your workbook. If it isn't, then change Worksheets(1).Activate in your RemoveComments Sub so that it refers to the correct worksheet.
Sub RemoveComments()
Worksheets(1).Activate
ActiveSheet.UsedRange.ClearComments
End Sub
Please see my reply
Sub delete_comments()
Dim i As Range
For Each i In ActiveSheet.UsedRange
i.ClearNotes
Next i
End Sub

Resources