关于批量处理某一列数值的问题

bj-alex 2014-01-14 10:35:08
Sub mysub()
Dim ShApp As Object, mysheet As Object
Dim TF As Boolean, i As Integer, j As Integer
Dim aTable As Object, n As Integer
Dim mypath, mypathtxt As String
On Error Resume Next
n = 0
mypath = ThisWorkbook.Path
mypathtxt = mypath & "\txt"
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "请选定要处理的excel文档"
.Filters.Add "excel文档", "*.xlsx"
.AllowMultiSelect = True
If .Show <> -1 Then Exit Sub
Set ShApp = GetObject(, "Excel.Application")
If Err <> 0 Then
TF = True
Set ShApp = CreateObject("Excel.Application")
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False

For i = 1 To .SelectedItems.Count
Set mysheet = ShApp.Workbooks.Open(.SelectedItems(i))
With mysheet.Sheets(1)
j = .[A65535].End(xlUp).Row

.Range(.Cells(1, 3), .Cells(j, 3)).Value = 1500
.Range(.Cells(1, 1), .Cells(j, 1)).Value = .Range(.Cells(1, 3), .Cells(j, 3)).Value * 3
Sheets(1).Copy
ActiveWorkbook.SaveAs Filename:=mypathtxt & "\" & i & ".txt", FileFormat:=xlText
ActiveWorkbook.Close
End With
n = n + 1
mysheet.Close True
Next i
End With
If TF = True Then ShApp.Quit
Set ShApp = Nothing
MsgBox "处理完毕,共处理了" & n & "个excel文档。"
Application.ScreenUpdating = True
End Sub

初次接触VBA,我需要在工作表中添加一列,并修改第一列的值,单步调试时,我令m=.Range(.Cells(1, 1), .Cells(j, 1)).Value * 3,m的值没发生变化,难道.Range(.Cells(1, 1), .Cells(j, 1)).Value * 3这种写法不对吗?
我尝试使用另一种方法,在第二个with里面添加For循环,结果总是报错,所end with找不到with。
请高手指点
...全文
149 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsd999 2014-01-16
  • 打赏
  • 举报
回复
引用 3 楼 qwangq 的回复:
[quote=引用 2 楼 dsd999 的回复:] 在第二个with里面添加For循环,结果总是报错,所end with找不到with。 这个for循环式怎么加的?把代码贴上来。
With mysheet.Sheets(1) j = .[A65535].End(xlUp).Row for m=1 to j if .cells(m,1)<>"" then .Cells(m, 1) = .Cells(m, 1) * 2.76243 .Cells(m, 2) = .Cells(m, 2) * 2.76243 end if .Range(.Cells(1, 3), .Cells(j, 3)).Value = 0 Sheets(1).Copy ActiveWorkbook.SaveAs Filename:=mypathtxt & "\" & i & ".txt", FileFormat:=xlText ActiveWorkbook.Close End With [/quote] for语句没有next啊?
bj-alex 2014-01-14
  • 打赏
  • 举报
回复
引用 2 楼 dsd999 的回复:
在第二个with里面添加For循环,结果总是报错,所end with找不到with。 这个for循环式怎么加的?把代码贴上来。
With mysheet.Sheets(1) j = .[A65535].End(xlUp).Row for m=1 to j if .cells(m,1)<>"" then .Cells(m, 1) = .Cells(m, 1) * 2.76243 .Cells(m, 2) = .Cells(m, 2) * 2.76243 end if .Range(.Cells(1, 3), .Cells(j, 3)).Value = 0 Sheets(1).Copy ActiveWorkbook.SaveAs Filename:=mypathtxt & "\" & i & ".txt", FileFormat:=xlText ActiveWorkbook.Close End With
dsd999 2014-01-14
  • 打赏
  • 举报
回复
在第二个with里面添加For循环,结果总是报错,所end with找不到with。 这个for循环式怎么加的?把代码贴上来。
dsd999 2014-01-14
  • 打赏
  • 举报
回复
m=.Range(.Cells(1, 1), .Cells(j, 1)).Value * 3

这句话是有问题的,所以你看到的m值没有变化。

把 On Error Resume Next 这句话注掉看看什么问题。

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧