Change formatting of a column to text - excel

Every time I run this code the entire sheet becomes Text.
I want to keep the rest of the sheet General, but change column A to Text.
For the
Columns("A").Select _
ActiveCell.NumberFormat = "#"
I tried
Range("A:A").Select
Selection.NumberFormat = "#"
I tried writing it with the specific workbook instead of letting VBA assume the active worksheet and cells.
I tried to NumberFormat the other columns to be General after running the formatting on column A.
Columns("A").Select
Range("A1").Activate
Selection.Delete Shift:=xlToLeft
Columns("A").Select
ActiveCell.NumberFormat = "#"
Range("A1").Select
ActiveSheet.Paste
Application.CommandBars("Office Clipboard").Visible = False
I expected that if I put another line after my numberformat that I would be able to get column B:ZZ to all be General.
I've gotten no error messages.

Just tested these, both work. Formulas referencing the affected cells do not compute, formulas referencing other numeric cells are calculating as normal.
Range("A:A").NumberFormat="#"
Range("A1").EntireColumn.NumberFormat="#"

Related

Autofill across and above a dynamic range

I'd like to autofill cells across and above a dynamic range.
I have a line of numbers in row 3 and would like to put the word "Customer No." in the cell above each one.
I do this by copying A2 and pasting into C2 then dragging across
Via VBA macro recorder the code I get looks like this
Selection.Copy
Range("C2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C2:E2"), Type:=xlFillDefault
Range("C2:E2").Select
I was wondering if there's a way to create an autofill across a dynamic range as the number of cells in row 3 will change from time to time?
Thanks
You could do it like this:
With ActiveSheet
.Range("C3", .Cells(3, Columns.Count).End(xlToLeft)).Offset(-1).Value = .Range("A2").Value
End With

Is there a way to delete cells used in a macro without messing up the relative references?

I am working on a spreadsheet where I recorded macros to fill a formula into a column in excel, but it does not fill every single cell--there are certain cells I skipped over because they are either blank or a subtotal within the column. Will it be possible to delete some rows without messing up the relative references used to record the macro?
I am hoping to be able to easily edit the code somehow, or maybe there is something I can put into the code to edit itself? The purpose of this spreadsheet is to avoid a lot of manual work, so having to pick through excel code and edit out the deleted rows would defeat the purpose of macros in the first place.
piece of VBA code:
ActiveCell.Offset(0, -5).Columns("A:E").EntireColumn.Select
Selection.EntireColumn.Hidden = False
ActiveCell.Offset(0, 2).Columns("A:B").EntireColumn.Select
Selection.EntireColumn.Hidden = True
ActiveCell.Offset(7, 3).Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-5]-RC[-1]"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A12"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A12").Select
ActiveWindow.SmallScroll Down:=3
ActiveCell.Offset(11, 0).Range("A1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A4"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A4").Select
ActiveWindow.SmallScroll Down:=6
ActiveCell.Offset(3, 0).Range("A1").Select
my layout:
The subtotals and empty cells need to be skipped. Each subtotal is a sum of the cells above it, and all the other cells have a simple =A1-B1, etc.
My recorded macro is meant to fill in the =A1-B1 down the cells on a certain day, and another day a similar macro will change the formulas to =B1-C1, and so on. Is there a way to make a macro change the formula based on what formula is already in the cell?

When I paste a column of numbers using PasteSpecial, it pasts them as text instead of numbers. How do I paste them in number format?

I am trying to copy a column of numbers in one sheet and paste them to another column in another sheet. When I paste the column of numbers, it pastes them as text even though the format of them in the original sheet is a number. What can I do?
I've tried using a couple different PasteSpecial formattings and I've also tried recording a macro where I convert the format from text to numbers, but neither of these worked.
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Worksheets("Data").Activate
Range("I1").Select
ActiveCell.Offset(NumRowsToSkip, 0).Select
ActiveCell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
I expected the column of numbers to be pasted as number, but instead they were pasted as text and it messes up my equations elsewhere in my spreadsheet.
You have a lot of selectiong going on that isn't needed. You should be able to set the formatting before pasting however the below code will automatically format the entire column to be whatever number format you specify in the macro.
Sub QuickExample()
'Set your number format the way you want. You can find this in formatting/custom to see whatever you've selected
'The below example is with commas, no decimals, red for negative.
Const YourPreferredNumberFormat As String = "#,##0_);[Red](#,##0)"
Range(Selection, Selection.End(xlDown)).Copy
With Worksheets("Data").Range("I1")
.Offset(NumRowsToSkip, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.EntireColumn.NumberFormat = YourPreferredNumberFormat
End With
End Sub

Is there a VBA string to select copied cells that used to have a blank formula?

I have an excel file where everything is controlled via a macro. At one point I am moving files from one sheet to another where this data is stored as a backlog.
I then try to select blank cells and remove the row if true. But cells appear blank but are not.
So I am moving it from Sheet A to Sheet B. The data in Sheet A is being moved and pasted as values in Sheet B. The data being moved has two columns: Column A holds Item ID and column B holds a date when the item cease to exist (Cease Date).
In Sheet A, Cease Date is populated through a simple formula (=+IF(O5<>"";O5;N5)) where if there is no new Cease Date input, fetch from backlog.
Now, if there neither is no new Cease Date input nor is there any backlog, the cell is blank.
When the macro copy and paste (as values) the data from Sheet A into Sheet B, column B is populated by blank cells (as intended) but there is something invisible, for lack of better word. Almost like there would be a formatting or like when you can encounter hidden characters not seen other than by ANSI.
IF I select any of the empty cells and press delete, then run "Go to special..." and blanks, the cell gets selected by the function.
I'm using this line to remove blanks:
Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
This is the segment of my code that copy/paste and handle the Cease Date section:
Sheets("Dashboard").Select
Range("B3:Q400").Select
Selection.Copy
Sheets("CeaseDate").Select
Range("F1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("G:T").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("F1:G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("F:G").Select
Selection.Delete Shift:=xlToLeft
Columns("B").SpecialCells(xlBlanks).EntireRow.Delete 'Remove rows that does not contain a Cease Date <--- This does not work since it wont treat the blank cells as blank
'Range converting to date format
Columns("B:B").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("A:B").Select
ActiveSheet.Range("A:B").RemoveDuplicates Columns:=Array(1, 2), _
Header:=xlYes
Selection.End(xlUp).Select
So I found a solution to this, I cannot explain why the problem occurs but apparently it has been something occurring in Excel since at least version 2003.
Problem: When I copy cells and use paste-special-value, the cells that appeared blank will fail the test of =ISBLANK() and returning a FALSE value. Yet, there is nothing to copy from the cell or anything to mark. If I select the apparently blank cell and press delete or Backspace, =ISBLANK() will now return a TRUE value.
Solution:
I select the area with the apparently blank cells
Open Find/Replace function and leave the Find What: blank (no spaces or anything)
and then in the Replace With: type in a string or word that you
KNOW does not appear anywhere else in the spreadsheet.
Click Replace All
All apparently blank cells will be replaced with the word
Now take the word and Find/Replace it with nothing
The replaced cells will now be truly blank and pass the =ISBLANK() test
I originally found this (quite obscure) solution here
If you have a cell that contains a formula, but the Value of that Formula is equivalent to ="", then it will display as Blank, and the COUNTBLANK function in Excel (or WorksheetFunction.CountBlank function in VBA) will call it Blank, but SpecialCells(xlBlanks) will not - because the Range.Formula property is not blank.
Here is a Function to retrieve cells with a Blank Value in a range:
Private Function GetNullValues(ByVal Target As Range) As Range
Dim TestingArea As Range, TestingCell As Range
For Each TestingArea In Target.Areas 'Loop through Areas in Target
For Each TestingCell In TestingArea.Cells 'Loop through Cells in Area
If TestingCell.Value = "" Then 'If Cell looks Blank
If GetNullValues Is Nothing Then 'If first blank found
Set GetNullValues = TestingCell 'Start list
Else 'If not first blank
Set GetNullValues = Union(GetNullValues, TestingCell) 'Add to list
End If
End If
Next TestingCell, TestingArea 'This is the same as doing 2 Next lines
End Function
Use it like this: Set BlankCells = GetNullValues(Sheet1.Columns(2))
What you could try is also removing formatting. I have encountered this several times and it worked for me with this code. with this line all formats in blank cells are removed.
Columns("B").SpecialCells(xlBlanks).EntireRow.ClearFormats
what you could also try is adding the .clearformats function to every delete part.
so if you have
sheet1.column("B").clearcontent
you could add
sheet1.column("B").clearcontent
sheet1.column("B").clearformats

Autofilter write in coding is not function like recorded in macro

I face a problem here.
formula = "=CONCATENATE(""ML"",MID(cell(C),2,1),MID(cell(C),4,5),""M"",RIGHT(cell(C),2),""_"",LEFT(D52,1),""_Q"")"
ActiveSheet.Range("$H$2:$H$1048576").AutoFilter Field:=8, Criteria1:="=SAMP"
'exclude the header
Range("A1").Select
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = formula
'Range("A2").AutoFill Destination:=Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
Selection.FillDown
I trying to filter my column H with this coding. After filter, I want to fill down those cells with formula. Is that any problem on my formula there, it can execute, but doesn't work like what I expected. And this codes although is same like the macros. but when run it, the data rows show not same like what I used macro recorded.
You are setting the .FormulaR1C1 property, which expects formulas to use R1C1 notation, while the string is written in normal A1 notation, so you should be setting the .Formula property.

Resources