Deleting a cell in a table in Word VBA - vba

I have created a table called "valtable2" and I am trying to delete the text in one of the cells.
With valtable2
.Cell(2, 1).Select
Selection.Delete
End With
With valtable2
.Cell(2, 1).Select
Selection.Delete
End With
The cell is being selected, but it doesn't delete.

Try to avoid using Select where possible. You could just set the cell value to nothing:
valtable2.Cell(2, 1).Range.Text = ""

Related

VBA Code to Copy to Bottom of Existing Data in Adjacent Field

My VBA Macro code is supposed to insert Column C, add the heading and then type a formula into cell C2 =text(B2," mm/dd/yyyy ") I then want to copy and paste that formula to the bottom of the data. The problem is that the range changes with each spreadsheet. I do I code it to look at column B and go to the end of the data and copy the formula up to Row 2?
Just learning to do Macros and need to modify the script.
Here is my code:
Sub TEST1()
'
' TEST1 Macro
'
'
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Range("C1").Select
ActiveCell.FormulaR1C1 = "Revised Billed Date"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],"" mm/dd/yyyy "")"
Range("C2").Select
Selection.Copy
Range("B2").Select
Selection.End(xlDown).Select
Range("C8").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Range("C7").Select
Selection.End(xlUp).Select
Range("C2").Select
End Sub
I believe that this will solve it for you. I will recommend you to learn to use With and End with to better organize and have a cleaner code. This will allow you to use .Range and .Paste .
All I did was pivot around the selected cells using .Offset(), place my self at the last row with content of column B, and create a range in column C in which I will lastly paste the content of the copied cell.
Sub TEST1()
With ActiveSheet
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
.Range("C1").Select
Selection.FormulaR1C1 = "Revised Billed Date"
.Range("C2").Select
Selection.FormulaR1C1 = "=TEXT(RC[-1],"" mm/dd/yyyy "")"
Selection.Copy
''''''Find last cell with content in column B and go back to column C
Selection.Offset(0, -1).End(xlDown).Offset(0, 1).Select
'''''Create a range from the end of column C up until below C2 cell
.Range(Selection, Selection.End(xlUp).Offset(1, 0)).Select
.Paste
End With
End Sub

excel vba copy row and paste to new sheet as pastevalue

I want to copy data from sheet "summary" row A44 (fixed row with dynamic data with formula) to sheet18 (row A3), A1 and A2 are header; i have below vba code and manage to do so. I would like to copy and paste the data as value (like Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False), so that the data will convert to absolute number, anyone how to edit the code?
Sub COPY_SUMMARY2COPYDATA()
Set des = Sheet18.Range("a1")
With Worksheets("SUMMARY")
.Rows(Range("A44").Row).Copy
des.Range("A3").Insert Shift:=xlUp
End With
Application.CutCopyMode = False
End Sub
Please try this:
Sub COPY_SUMMARY2COPYDATA()
Dim LastRow As Long
LastRow = Sheet18.Cells(Rows.Count,1).End(XlUp).Row + 1
Sheets("SUMMARY").Rows("44").Copy
Sheet18.Rows(LastRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Hope this help
To find the first blank cell in column A look from the bottom up and offset down one row.
Use a direct value transfer with .Value2 to pass over 'the data will convert to absolute number'. This will discard regional currency and date conventions as well as formulas in favor of the raw underlying value.
with Worksheets("SUMMARY")
with intersect(.usedrange, .rows(44).cells)
Sheet18.cells(.rows.count, "A").end(xlup).offset(1, 0).resize(.rows.count, .columns.count) = .value2
end with
end with

Excel vba MsgBox to display message when duplicate is found

I am trying to remove duplicates from a table in Excel, I have a piece of code that removes duplicates without any problem, I am wondering if I could make it prompt a message box when a duplicate is found saying something along the lines "This entry is a duplicate entry" Any suggestions? This Is what I got so far:
Sub AccessTransfer()
Range("A1:F1").Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 6).Value = "Oven"
Range("A65536").End(xlUp).Offset(1, 0).Select
Call GoDupe
Sheets("Sheet1").Select
Application.CutCopyMode = False
End Sub
Sub GoDupe()
Cells.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Range("A65536").End(xlUp).Offset(1, 0).Select
End Sub
Rather than looping through, identifying and prompting for each duplicate, you could simply highlight all duplicates and prompt the user once.
Your GoDupe() sub could look something like this:
Sub GoDupe()
Cells.FormatConditions.AddUniqueValues
With Cells.FormatConditions(Cells.FormatConditions.Count)
.DupeUnique = xlDuplicate
.Interior.Color = RGB(255, 0, 0)
End With
If MsgBox("Red highlighted cells are duplicated. OK to remove duplicates?", vbOKCancel) = vbOK Then
Cells.RemoveDuplicates Columns:=Array(1), Header:=xlNo
Range("A65536").End(xlUp).Offset(1, 0).Select
End If
Cells.FormatConditions(Cells.FormatConditions.Count).Delete
End Sub

We can't paste Excel ranges because the copy area and paste area aren't the same size

I would like to loop through column A in Worksheet1 and find the first cell which has a specified text "Oil Production". This cell is the first cell in the array I wish to copy to Worksheet2. This cell and the size of the array will change from time to time, hence the code I have used. I then paste it into cell B7 in Worksheet2 which will never change.
This is my formula. I get the error at line ActiveSheet.Paste
Sub Test()
Application.ScreenUpdating = False
For Each Cell In Sheets("Sheet1").Range("A:A")
If Cell.Value = "Oil Production" Then
ActiveSheet.Cells.Select
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
Selection.Copy
Sheets("Sheet2").Select
Range("B7").Select
ActiveSheet.Paste
End If
Next
End Sub
resize the area:
Sub Test()
Dim MyRowCount As Long, MyColCount As Long
Application.ScreenUpdating = False
For Each Cell In Sheets("Sheet1").Range("A1:A" & Range("A" & Rows.count).end(xlup).row) 'This make it poll the used data rather than the whole column
If Cell.Value = "Oil Production" Then
ActiveSheet.Cells.Select
With Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).column))
.Copy
MyRowCount = .Rows.Count
MyColCount = .Columns.Count
End With
Sheets("Sheet2").Select
Range("B7").Resize(MyRowCount, MyColCount).PasteSpecial xlPasteAll
'Do you need to flick back to Sheet1 after pasting?
End If
Next
End Sub
Also I took out a bunch of selects for you.
Range("A1").Select
Selection.Paste
can be written as
Range("A1").PasteSpecial XLPasteAll
You can chop out most selects this way, you can see I have also done it with the Range you are copying

How to prevent excel macro from pasting empty rows

I am trying design a macro in excel 2007. Here is what I need it to do:
When I enter an ID into a specific cell and run the macro, it will search for that ID in column A of a different workbook and autofilter. Then I need it to copy that data and paste it into the first workbook. My code is working, however when it is pasting a ton of extra rows beneath my data. How can I make it only copy and paste data and not empty rows? Here is my code:
Sub Medications()
'
' Medications Macro
'
' Keyboard Shortcut: Ctrl+m
'
Range("B1").Select
Workbooks.Open Filename:= _
"I:\Pharmacy\MTMP\2013\Master Lists\CMR Medication List.xlsx"
Range("A1").Select
ActiveCell.FormulaR1C1 = "Member ID"
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range(Selection, ActiveCell.SpecialCells(xlLastCell)).AutoFilter Field:=1, Criteria1:=Workbooks("Standardized Format Spreadsheet.xlsm").Worksheets("Demographics").Range("B1").Value
Cells.Select
Selection.Copy
Windows("Standardized Format Spreadsheet.xlsm").Activate
Sheets("Detailed Medication List").Select
Range("A1").Select
ActiveSheet.Paste
Windows("CMR Medication List.xlsx").Activate
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Sheets("Demographics").Select
End Sub
It is always best to avoid selecting items whenever possible. You can use set a workbook to an object and access it through that.
The reason you are getting extra cells when you copy/paste is because you are selecting every cell and then copying. I suggest using only the used range so you don't pick up any extra cells.
Sub Medications()
Dim CMR_Wkbk As Workbook
Dim SFS_Wkbk As Workbook
Set SFS_Wkbk = Workbooks("Standardized Format Spreadsheet")
Set CMR_Wkbk = Workbooks.Open("I:\Pharmacy\MTMP\2013\Master Lists\CMR Medication List.xlsx")
Range("A1").Value = "Member ID"
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:=SFS_Wkbk.Sheets("Demographics").Range("B1").Value
ActiveSheet.UsedRange.Copy Destination:=SFS_Wkbk.Sheets("Detailed Medication List").Range("A1")
Application.DisplayAlerts = False
CMR_Wkbk.Close
Application.DisplayAlerts = True
Sheets("Demographics").Select
End Sub
Cells.Select
Selection.Copy
Cells.Select is selecting the entire content of the worksheet. I don't know, obviously, what you sheet looks like, but try selecting only the CurrentRegion - the equivalent of what is highlighted when you click in a cell and press Ctrl-A:
ActiveCell.CurrentRegion.Copy