I can't seem to get FinalRow to declare? I am trying to put in a totals row, but it just skips over that bit of code. I am stumped, as I have never had this problem before.
Option Explicit
Sub CopyData()
Dim Val As Range, rnge As Range, wbDataPath As String
Dim wsDest As Worksheet, wsData As Worksheet, wbData As Workbook
Dim FinalRow As Long, FinalCol As Long
'Prompts window to open file
wbDataPath = Application.GetOpenFilename("Excel & CSV Files, *.xls*; *.csv")
If wbDataPath = "False" Then Exit Sub
Application.ScreenUpdating = False
'set each object
Set wsDest = ActiveWorkbook.ActiveSheet
Set wbData = Workbooks.Open(wbDataPath)
Set wsData = wbData.Sheets(1)
FinalRow = wsDest.Cells(Rows.Count, 1).End(xlUp).Row
FinalCol = wsDest.Cells(2, Columns.Count).End(xlToLeft).Column
With wsData
'Unhide Columns
.Columns.EntireColumn.Hidden = False
'Find not needed columns and delete
For Each Val In .UsedRange
If Val.Value = "選択" Or Val.Value = "利用者コード" Or Val.Value = "フリガナ" Or Val.Value = "部屋コード" Or Val.Value = "部屋グループコード" Or Val.Value = "部屋グループ名称" Or Val.Value = "介護保険者番号" Or Val.Value = "被保険者番号" Or Val.Value = "保険者名称" Or Val.Value = "広域番号" Or Val.Value = "広域名称" Or Val.Value = "介護度コード" Or Val.Value = "介護度名称" Or Val.Value = "地区コード" Or Val.Value = "地区名称" Or Val.Value = "医療保険者番号" Or Val.Value = "記号" Or Val.Value = "番号" Or Val.Value = "契約日数" Or Val.Value = "サービス実数" Or Val.Value = "外泊日数" Then
If rnge Is Nothing Then Set rnge = Val Else Set rnge = Union(rnge, Val)
End If
Next Val
If Not rnge Is Nothing Then rnge.EntireColumn.Delete
' Copy contents to destination sheet
.Range("A:A").SpecialCells(xlConstants).EntireRow.Copy
wsDest.Range("A1").PasteSpecial xlPasteAll, Transpose:=True
End With
wbData.Close False 'close opened file with no changes
'Add Total Row to wsDest sheet
'wsDest.Cells(45, 1).Value = "Total"
'wsDest.Range(Cells(45, 2), Cells(45, FinalCol)) _
.FormulaR1C1 = "=Sum(R[-42]C:R[-2]C)"
wsT.Cells(FinalRow + 2, 1).Value = "Total"
wsT.Range(Cells(FinalRow + 1, 2), Cells(FinalRow + 1, FinalCol)) _
.FormulaR1C1 = "=Sum(R[" & -FinalRow + 2 & "]C:R[-2]C)"
With wsDest
.Columns("B").EntireColumn.Delete
.Columns("A:CC").EntireColumn.AutoFit
.Rows.EntireRow.RowHeight = 13
End With
wsDest.Range("B3", "CC10000").Style = "Comma [0]"
Range("A2:BX2615").Select
With Selection.Font
.Name = "MS 明朝"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
End With
Application.ScreenUpdating = True
End Sub
The first workbook holds the data, The second brings in the data.You will have to take of the comment tags to get the final row peice of code to run. I have also hard coded the formula to see if it would work, that is why there is two pieces of code doing exactly the same thing.
Bookmarks