如何用VBA 向Word表格中插入值, 又如何判断表格中的值是否为空呢? wj-wangjun

 

图片

文件名称:

密级:

 

版次/ 修订状态:

A/0

文件编号:

Page 1 of 3

以上表格为word 页眉中的表格
========要判断 以上表格中如果有空时,则弹出对话框页面==========
' 当打开Word时弹出对话框 要用户输入内容
Private   Sub Document_Open() Sub Document_Open()

Dim flag As Boolean
    flag 
= True
    
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Range
       
For i = 2 To 9 Step 1
        
If Len(.Cells(i).Range.Text) = 2 Then
            flag 
= False
        
End If
        
       
Next
       
If flag = False Then
          AddHeader.Show
        
End If
    
End With
End Sub

=====点确定后将页面对话框中的值 插入到word 的页眉表格中====
Private   Sub CommandButton1_Click() Sub CommandButton1_Click()
With ActiveDocument.Sections(1)
'Cell(4, 2).Range.InsertBefore (TextBox4.value)
    ' .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 2).Range.InsertAfter ("aaa")
  .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(13).Range.Delete
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(13).Range.InsertAfter (TxtFileName.Value)
  
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(22).Range.Delete
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(22).Range.InsertAfter (TxtSecrecy.Value)
   
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(24).Range.Delete
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(24).Range.InsertAfter (TxtRev.Value)
  
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(26).Range.Delete
  .Headers(wdHeaderFooterPrimary).Range.Tables(
1).Cell(26).Range.InsertAfter (TxtID.Value)
  
    
'页尾的做法
'
    .Footers(wdHeaderFooterPrimary).Range.Text = "Copyright  BYD Company Limited"
End With
AddHeader.Hide
End Sub


 ========提问 =================
比如说sheet1 中A1:A10不为空,用代码判断不为空后,光标移向A11,并且A11=11,B11=22
再录入数据后光标又光标移向A12,并且A12=22,B12=23,依此类推

========解决方法:===============================
Sub Text()
 If IsEmpty([A1]) Then
 [A1] = "你的数据"
 Else
 [A65536].End(xlUp).Offset(1, 0).Value = "你的数据"
 End If
End Sub  
 
 ========提问 =================
判断word中表格是否为空
========解决方法:===============================

' a.Cells.Count=41,46,41+5*7
Dim  list%
' 你的要求可能是这样
With  ActiveDocument.Tables( 1 ).Range
    
For  i  =   41   To   41   +   5   *   7   Step   5   ' 测得第一项的后面对应的是第41个单元格,相差是5。所以,step=5
        list  =  list  +   1   ' 利用list来得到是第几项没有加项
          If   Len (.Cells(i).Range.Text)  =   2   Then     ' 因为类回车加+竖线加在一起是2个长度。所以。。这样判断
             ' n = .Cells(i - 4).Range.Text  '取得单元格的内容+回车+竖线
             ' n = Mid(n, 1, Len(n) - 2)  '去掉回车+竖线的,即我们所看到的内容
             MsgBox   " 提示:没有给第 "   &  list  &   " 项打分! " , vbOKOnly,  " www.excelhome.net "
            .Cells(i).Range.Select  
' 选中所在的单元格
             Exit Sub
          
End   If
    
Next
End   With

转载于:https://www.cnblogs.com/wj-wangjun/archive/2007/08/24/868593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值